Well I ditched my idea to load, store, and eval() templates from files as it has some serious security issues, and decided to go with a much simpler approach:
class template {
public $theme = "default";
public $theme_dir = "themes/";
private $templates = array();
private $vars = array();
private $errors = array();
private $cwd = "";
function template() {
$this->cwd = getcwd();
}
function assign($var_name, $var_value) {
if (!get_magic_quotes_gpc()) {
$var_value = addslashes(&$var_value);
}
$var_value = str_replace(";","",$var_value);
$var_value = preg_replace("/[^A-Za-z0-9_-]\n\t\r/","",$var_value);
$this->vars[$var_name] = $var_value;
}
function display($file) {
//chdir("../".$this->theme_dir);
//Takes about 20 milliseconds to chdir
$filename = $this->theme_dir.$this->theme."/".$file.".tpl.php"; //Add "../". to test locally
extract($this->vars);
ob_start();
include($filename);
$contents = ob_get_contents();
ob_end_clean();
print $contents;
return 1;
}
}
However, I'm having some troubles... How can I access functions from this class inside my templates? I obviously can't extract() the class itself seeing as its not an array... And I can't exactly use $this-> or $template, seeing as ... Well, I honestly have no idea why I can't do this.
So whats a solution? What about accessing other classes? What about accessing just a function, completely outside of a class?