Created
September 7, 2011 22:19
-
-
Save tchalvak/1201969 to your computer and use it in GitHub Desktop.
PHP inline templating craziness.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// System for using simple, native php templates. | |
// TODO: Put this in a namespace to keep it clean and seperate. | |
function display_page_inline($templates, $options, $data){ | |
$title = @$options['title']; | |
echo "<html><head><title>".htmlentities($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>"; | |
} | |
?> | |
<?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 htmlentities($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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment