目前的情况
我正在使用这个函数:
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.');
}
}基本上,它只是用$tpl->name的数据取代了来自tpl的$tpl->name。
问题是
我希望开始使用{$name},而不是在TPL文件中使用<?php echo $name; ?>。有谁能为我指出用$tpl->name的数据来代替它的正确方向吗?
我试过的
我尝试用regex来完成它,试图找到{和},然后替换它,但是它只将$name输出为文本。
发布于 2013-11-04 16:40:09
我可能会用正则表达式,但这是另一种。您还可以通过$this->vars键预先判断:
$template = file_get_contents($file);
$vars = explode(',', '{$'.implode('},{$', array_keys($this->vars)).'}');
$output = str_replace($vars, $this->vars, $template);发布于 2013-11-04 15:54:22
像这样吗?
preg_replace("/\{(.+)\}/e", "$1", $template);注意,如php手册上所说的,从5.5开始不推荐使用:http://php.net/manual/en/reference.pcre.pattern.modifiers.php
https://stackoverflow.com/questions/19771404
复制相似问题