0

这个有点奇怪。我创建了一个函数,用于选择模板并包含它或解析 %0、%1、%3 等变量。这是当前功能:

if(!fopen($tf,"r")){
    $this->template("error",array("404"));
}
$th = fopen($tf,"r");
$t = fread($th, filesize($tf) );
$i=0;
for($i;$i<count($params);$i++){
    $i2 = '%' . $i;
    $t = str_replace($i2,$params[$i],$t);
}
echo $t . "\n";
fclose($th);

其中 $th 是我的模板文件的相对目录。我的问题是,我需要在这些文件中执行 PHP,同时能够替换字符串变量 %0 %1 等。我该如何尝试呢?

4

1 回答 1

0

就像我在评论中所说的那样,我认为像 Smarty 这样的模板引擎可能会为您提供更好的服务,但这是我如何使用输出缓冲而不是eval()

像这样的东西

ob_start();
include "your_template_file.php";
$contents = ob_get_contents();  // will contain the output of the PHP in the file
ob_end_clean();

// process your str_replace() variables out of $contents
于 2013-03-29T03:25:31.900 回答