PHP自定义模板引擎

类文件my/config.class.php

arr[$name]=$value;
}
//调用模板
public function display($temp)
{
//模板文件路径
$path="templaces/";
//编译文件路径
$com="templaces_c/";
//模板文件名字
$comfile=$com.$temp.'.php';
/*1.如果编译文件不存在需要重新生成编译文件
2.如果模板文件发生了改变,需要重新生成编译文件
*/
if(!file_exists($comfile)||filectime($path.$temp)>filectime($comfile)){//判断模板文件是否存在(如果文件不存在就创建)
//获取模板源代码
$str=file_get_contents($path.$temp);
//查找模板中的变量(模糊匹配)
//正则----$reg = '/\{\s*\$([a-zA-Z_]\w*)\s*\}/';
$reg='/\{\s*\$([a-zA-Z_]\w*)\s*\}/';
/*替换成arr['']?> 模式单元双引号加\\*/
$place="arr['\\1'] ?>";
//获取替换后的代码
$html=preg_replace($reg,$place,$str);
//编译文件(模板文件.php)
file_put_contents($comfile,$html);
}
include_once$comfile;
}
}

模板文件my/templaces





Title


{$title} {$hello}



引用文件my/templaces_c
assign('hello',$title);
$smarty->assign('content',$content);
$smarty->assign('haha',$aa);
$smarty->display('01.html');

编译文件0.1.php

assign('hello',$title);
$smarty->assign('content',$content);
$smarty->assign('haha',$aa);
$smarty->display('01.html');

你可能感兴趣的:(PHP自定义模板引擎)