I'm using this function:
public function fetch($file){
$file = $this->options['template_dir'].(string)$file;
if(file_exists($file)){
ob_start();
if($this->options['extract']){
extract($this->vars);
}
include $file;
return ob_get_clean();
}else{
throw new Exception('De ('.$file.') template bestaat niet.');
}
}
Basically it just replaces <?php echo $name; ?>
from the tpl with the data of $tpl->name
.
I would like to start using {$name}
instead of using <?php echo $name; ?>
in my TPL files. Can anyone point me in the right direction for replacing this with the data of $tpl->name
?
I tried to do it with a regex, trying to find the { and } and then replace it, but it will only output $name
as text.
I would probably go with a regex, but here is an alternate. You could also foreach through $this->vars keys:
$template = file_get_contents($file);
$vars = explode(',', '{$'.implode('},{$', array_keys($this->vars)).'}');
$output = str_replace($vars, $this->vars, $template);
Something like this?
preg_replace("/\{(.+)\}/e", "$1", $template);
note that is deprecated from 5.5 as said on php manual: http://php.net/manual/en/reference.pcre.pattern.modifiers.php