In my own (crappily done) template class I use the curly braces (though there is an option to change them to anything you want) style in the template.
You feed variables to the thing and it figures out things on it's own.. It's kind of hard to explain in words soo here's an example of how I use my class
the template (template.tpl)
Code: Select all
<html>
<body>
<div class="menu">{menu}</div>
<div class="body">{body}{/div>
<div class="ads">{ads}</div>
</body>
</html>
the code
Code: Select all
<?php
include ( 'template.class.php' );
$template = new Template();
//by defualt it's { }, so it doesn't need to be set, but I set it here for an example of how I could change the tags
$template -> set ( 'template char', '{', '}' );
//in this array, the array key would be the things inside the braces, and it's value
$vars = array();
$vars['menu'] = 'this is a menu';
$vars['body'] = 'this is the body';
//adds the array made above
$template -> add_vars ( $vars );
//adds a single variable, no need for an array
$template -> add_var ( 'ads', 'these are the ads' );
//sets the template file to use
$template -> add_template_file ( 'template.tpl' );
//outputs page
$page_output -> compile_page ();
$page_output -> output_compiled_data();
?>
And that's how it does the setting up, and here are some code snippets from the class to see how it works
This gets the variables from the array and uses the keys to get the thingies
Code: Select all
<?php
//obviously compiles page then destroys template files
function compile_page ()
{
$page = $this -> _parse_string;
$template_array = $this -> _template_array;
if ( $this -> _loops == 'on' )
{
$page = $this -> _parse_loops ( $page );
}
$page = 'print ''' . $page;
$page = $page . ''';';
$page = str_replace ( $this -> _start_string, '''; print $template_array[''', $page );
$page = str_replace ( $this -> _end_string , ''']; print ''', $page );
$this -> _compiled_page = $this -> _compiled_page . $page;
$this -> destroy( 'template' );
return true;
}
?>
this is what would change some of the settings for the class
Code: Select all
<?php
function set ( $setting, $option_one, $option_two = NULL )
{
//settings here
switch ( $setting )
{
case 'template char':
$this -> _start_string = $option_one;
$this -> _end_string = $option_two;
break;
case 'loops':
$this -> _loops = $option_one;
break;
}
return true;
}
?>
and the output is done with a simple eval() line
Code: Select all
<?php
function output_compiled_data ()
{
$template_array = $this -> _template_array;
eval ( $this -> _compiled_page );
$this -> destroy( 'compiled data' );
return true;
}
?>
That's how my template class is set up (the full class is alot longer, and loops are too buggy to be used, but meh). Anytime I need templating I use it.
Your idea works too, but only if you already know beforehand what variables you want to be replaced by the engine. and I'm sure there are alot diffrent and better ideas than the one I use aswell.