Templates!!!
Posted: Thu Aug 27, 2009 5:08 pm
Ok...I've been thinking long and hard about this...
Before now, I've always echo'd HTML and stuff from within my PHP scripts. This was ok, because my site has been pretty small..but it's getting a bit bigger now, and I think it'd be easier to maintain with templates.
Anyway, I've been thinking about how to construct and parse template files. The idea is to keep logic in the scripts, and display stuff in the templates. I came to realise that PHP can in fact do both of these very well, without having to create a new language for templates.
So anyway, this is the templating system I've come up with. This is the main class:
Here's an example of how it would be used:
The script:
And template_file.template.php
Anyway, I was just hoping to get some feedback. Anything that could be improved? Anything that you would do differently? I'm not sure if I should have static methods (like I do currently) so that child templates can inherit variables from parent templates...thoughts? 
Also, I currently have to use output buffering to catch the template (since I close PHP tags)...is there a better way of doing this?
Yeah, and is it OK to supply the template with all defined variables? I only do this so that I don't have to repeatedly assign variables that are used often...for example, if I have a variable $file_path which is used in every link, simply using that from the $globals object would be a lot simpler than assigning it to every template, don't you think?
Anyway, yeah, all opinions appreciated
Thanks,
Jack.
Before now, I've always echo'd HTML and stuff from within my PHP scripts. This was ok, because my site has been pretty small..but it's getting a bit bigger now, and I think it'd be easier to maintain with templates.
Anyway, I've been thinking about how to construct and parse template files. The idea is to keep logic in the scripts, and display stuff in the templates. I came to realise that PHP can in fact do both of these very well, without having to create a new language for templates.
So anyway, this is the templating system I've come up with. This is the main class:
Code: Select all
class template
{
private static $vars = array();
public static function assign_vars(array $vars)
{
self::$vars = array_merge(self::$vars, $vars);
}
private static function assign_global_vars()
{
$stdClass = new stdClass;
foreach($GLOBALS as $key => $value)
{
$stdClass->$key = $value;
}
return $stdClass;
}
public static function compile($template, $function)
{
global $config_init;
//flush current output buffer
ob_end_flush();
//start output buffering to catch template
ob_start();
//execute template
$file = $_SERVER['DOCUMENT_ROOT'].'/templates/'.$template.'.template.php';
if(file_exists($file))
{
include $file;
if(function_exists($function) && is_callable($function))
{
echo $function((object) self::$vars, self::assign_global_vars());
}
else
{
trigger_error('Template function '.$function.' is non-existent!', E_USER_ERROR);
}
}
else
{
trigger_error('Template file '.$file.' is non-existent!', E_USER_ERROR);
}
//get template contents
$ob = ob_get_contents();
//end output buffering
ob_end_clean();
//restart output buffering
ob_start(((bool) $config_init->get_config('compress_output_buffer')) ? 'ob_gzhandler' : null);
//return contents
return $ob;
}
}The script:
Code: Select all
include 'template_class.php';
$some_var = 'hello world';
template::assign_vars(array(
'hello' => $some_var
));
echo template::compile('template_file', 'template_function');Code: Select all
<?function template_function($vars, $globals){?><html><body><h1><?=$vars->hello?></h1></body></html><?}?>Also, I currently have to use output buffering to catch the template (since I close PHP tags)...is there a better way of doing this?
Yeah, and is it OK to supply the template with all defined variables? I only do this so that I don't have to repeatedly assign variables that are used often...for example, if I have a variable $file_path which is used in every link, simply using that from the $globals object would be a lot simpler than assigning it to every template, don't you think?
Anyway, yeah, all opinions appreciated
Thanks,
Jack.