für ein kleines Projekt brauche ich eine einfache Template Engine, habe aber nichts gefunden. Also habe ich mich mal selber dran gemacht

Code: Alles auswählen
/**
* The template engine
*/
class template
{
var $path;
var $filename;
var $uncopiled_code;
var $copiled_code;
var $vars = array();
function template($path)
{
if ( is_dir($path) && !empty($path) )
{
$this->path = $path;
return true;
}
else
{
die('<strong>Error: <strong> $template !<br />wrong path!');
return false;
}
}
function set_filename($filename)
{
if ( !empty($filename) )
{
$this->filename = $this->path . $filename;
if ( !is_readable($this->filename) )
{
die('<strong>Error: <strong> $template->set_filename !<br />File isn\'t readable!');
return false;
}
return true;
}
else
{
die('<strong>Error: <strong> $template->set_filename !<br />Filename is empty!');
return false;
}
}
function set_vars($vars)
{
if ( is_array($vars) && !empty($vars) )
{
$this->vars = array();
//
// From www.php.net ;)
//
reset ($vars);
while ( list($key, $val) = each($vars) )
{
$this->vars[$key] = $val;
}
return true;
}
else
{
die('<strong>Error: <strong> $template->set_vars !<br />$vars is no array or is empty!');
return false;
}
}
function parse()
{
$this->uncopiled_code = file_get_contents($this->filename);
reset ($this->vars);
while ( list($key, $val) = each($this->vars) )
{
$this->copiled_code = str_replace('{' . $key . '}', '<?php echo \'' . $val . '\'; ?>', $this->uncopiled_code);
}
eval( '?>' . $this->copiled_code . '<?php' );
}
}
In der Datei, die die Template Engine nutzt steht folgendes:
Code: Alles auswählen
$path = './';
require($path . 'template.php');
$template_path = $path . 'styles/' . $config['style'] . '/';
$template = new template($template_path);
$template->set_filename('header.html');
$template->set_vars(array(
'PAGE_TITLE' => 'Hallo ;)',
'SITE_NAME' => 'Seiten Titel',
'SITE_DESCRIPTION' => 'Beschreibung'
));
$template->parse();
Code: Alles auswählen
{PAGE_TITLE}<br />
{SITE_NAME}<br />
{SITE_DESCRIPTION}<br /><br />
wisst ihr, was ich falsch mache, bzw. was ich verbessern könnte?
Johannes