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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.