I'm doing something quite strange. I'm trying to create a tiny system for creating templates inline in PHP, so that I can refactor by creating multiple templates in the original script, and eventually move the templates to separate scripts.
Here's the silly/basic template library that just calls an anonymous function that gets passed in after some HTML header stuff, and then calls a footer:
<?php
// System for using simple, native php templates.
// TODO: Put this in a namespace to keep it clean and separate.
function display_page_inline($templates, $options, $data){
$title = @$options['title'];
echo "<html><head><title>".$title."</title></head>";
if(is_array($templates)){
foreach($templates as $template_function){
$template_function($data);
}
} else{
$template($data); // Call & display the template.
}
echo "<div>Footer</div>";
echo "</html>";
}
?>
Here's where it's going to get strange. In the scripts I'm creating anonymous functions, saved to variables, and then passing them to the template library for outputting. To avoid having to call an array element (and check whether the element exists each time), I'm trying to export the array passed in to get standard variables in the template (e.g. $name
instead of $data_array['name']
). Is there a way to abstract away this behavior in PHP?
<?php
require_once('core.php');
// Inclusion of the template library
require_once('../core/inline_templates.php');
// Initialization Logic
$title = 'Bob';
// Decide what will be able to be output and put it into an array to pass to the template
$template_output_variables = array('name'=>'no name');
// The page gets displayed at the bottom.
// =================== Templates ========================================
// Todo: Potentially namespace the stuff here.
// Define the anonymous template function here, has html and such in native php here.
$contact_us = function ($template_output_variables=null){
extract($template_output_variables); // Pull the variables.
// Leave php in the template function.
?>
<body>
Hello World Body Text, Hello <?php h($name);?>.
</body>
<?php
}; // End of template function declaration.
$killer_section = function ($data=null){
extract($data); // Pull the variables.
?>
Breaking news, There is a killer among us.
<?php
}; // End of killer section anon. function.
// Display the page.
// call & display the template via delayed execution, display it within a pre-set head and footer.
display_page_inline($templates=array($contact_us, $killer_section), $options=array('title'=>$title), $template_output_variables);
?>
It's very strange, I know, but I'm trying to do a whole new approach to refactoring, and make use of some new PHP 5.3 features while doing it. The goal is to have inline templates that I can put in any script, as many as I want, and move anywhere I want later, or merge the templates, etc.