I have a function called load_template()
this function has two parameters
- $ name => template name
- $ vars => array of key variables => to be replaced in the template.
how I want it to work.
in the template ('test') I want to be able to write
<?php echo $title; ?>
then call
load_template('test', array('title' => 'My Title'));
and fill it out.
How can i do this?
Output buffering method. I came up with the code below.
I am sure that it can be improved.
public static function template($name, $vars = array()) { if (is_file(TEMPLATE_DIR . $name . '.php')) { ob_start(); extract($vars); require(TEMPLATE_DIR . $name . '.php'); $contents = ob_get_contents(); ob_end_clean(); return $contents; } throw new exception('Could not load template file \'' . $name . '\''); return false; }
source share