我正在尝试创建一个模板函数:
function includeAndTemplate($file){
$json = '{"FIRST_NAME" : "First Name", "MIDDLE_NAME" : "Middle Name"}';
$template_vars = json_decode($json);
$file = file_get_contents($file);
foreach($template_vars as $var => $value){
$file = str_replace("{{'$".$var."}}", $value, $file);
}
return $file;
}
我的html/php:
<p>{{$FIRST_NAME}}, {{$MIDDLE_NAME}}</p>
这很好用。
现在,如果我有这个会发生什么:
<p>{{$FIRST_NAME}}, {{$MIDDLE_NAME}}, the time now is <?php echo data("H:i:s"); ?></p>
原始php代码输出到客户端
我想你要找的是这样的东西:
<?php
function includeAndTemplate($file){
$json = '{"FIRST_NAME" : "First Name", "MIDDLE_NAME" : "Middle Name"}';
$template_vars = json_decode($json);
ob_start();
require $file;
$file = ob_get_clean();
foreach($template_vars as $var => $value){
$file = str_replace("{{'$".$var."}}", $value, $file);
}
return $file;
}
只需包含该文件,就可以执行PHP代码,然后替换您想要替换的任何内容。