<?php
class MinSmarty{
// 模版文件的路径
var $template_dir = "./templates/";
// 模版文件被替换后的文件 命名格式为com_对应的tpl.php
var $complie_dir = "./templates_c/";
// 存放变量值
var $tpl_vars = array();
// 这里使用两个方法实现assign 和 display
function assign($tpl_var,$var=NULL){
if($tpl_var!=NULL){
$this->tpl_vars[$tpl_var]=$var;
}
}
// 这里编写display方法的实现
function display($tpl_file){
// 读取这个模版文件->替换可以运行的php文件(编译后文件)
$tpl_file_path=$this->template_dir.$tpl_file; // 模版文件的路径
$complie_file_path=$this->complie_dir."com_".$tpl_file.".php"; //编译后的文件路径
// 判断文件是否存在
if(!file_exists($tpl_file_path)){
return false;
}
// 不让每次执行都生成编译文件
if(!file_exists($complie_file_path) || filemtime($tpl_file_path)>filemtime($complie_file_path)){
$fp1_file_con=file_get_contents($tpl_file_path); // 获取模版文件的全部内容
// 这里进行正则替换把 模版文件中的代码 {$title} 替换成 <?php echo $this->tpl_vars['title'];? >
$pattern=array(
'/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i'
);
$replace=array(
'<?php echo $this->tpl_vars["${1}"];?>'
);
$new_str=preg_replace($pattern,$replace,$fp1_file_con); // 替换后的内容
file_put_contents($complie_file_path,$new_str); // 替换后的内容生成一个php文件
}
// 引入编译后的文件
include_once("$complie_file_path");
}
}
?>
<?php
include_once("MySmarty.class.php");
$title="这里是标题";
$content="这里是内容111111";
$MySmarty=new MySmarty();
$MySmarty->assign("title",$title);
$MySmarty->assign("content",$content);
$MySmarty->display("intro.tpl");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{$title}</title>
</head>
<body>
{$content}
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有