Jump to content

[SOLVED] 'exactract()'ing functions, classes, etc. into a template system


Nolongerused3921

Recommended Posts

Well I ditched my idea to load, store, and eval() templates from files as it has some [b]serious[/b] security issues, and decided to go with a much simpler approach:
[code]
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;
}
}
[/code]

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 [b]just[/b] a function, completely outside of a class?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.