Skip to content

Instantly share code, notes, and snippets.

@tchalvak
Created September 7, 2011 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchalvak/1201969 to your computer and use it in GitHub Desktop.
Save tchalvak/1201969 to your computer and use it in GitHub Desktop.
PHP inline templating craziness.
<?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