【smarty项目源码】模拟smarty模版文件的解析过程
2015-11-06 06:39
302 查看
[php] view plaincopy
<?php
class MyMiniSmarty{
//模版文件的存放路径
var $template_dir="./templates/";
//编译文件的存放路径 ,编译文件的名称格式暂定为:com_对应的tpl.php
var $complie_dir="./templates_c";
//模版变量数组:存放所有模版变量的值
var $tpl_vars=array();
//这里我们主要模拟两个方法
//参数1-模版变量 参数2:模版变量的值
function assign($tpl_var,$val=null){
if($tpl_var!=''){
$this->tpl_vars[$tpl_var]=$var;
}
}
//这里编写display
//参数1-要显示的模版文件名
function display($tpl_file){
//模版文件的路径
$tpl_file_path=$this->template_dir.$tpl_file;
//编译文件的命名及路径
$complie_file_path=$this->complie_dir."com_".$tpl_file.".php";
//判断当前模版文件是否存在
if(!file_exists($tpl_file_path)){
//如果当前模版文件不存在,则返回false
return false;
}
//查看是否有编译文件,如果没有编译文件,或者说模版文件的修改时间大于编译文件的生成时间,则需要重新编译
if(!file_exists($comlie_file_path) ||filemtime($tpl_file_path)>filemtime($complie_file_path)){
//获取模版文件的内容
$tpl_file_content=file_get_contents($tpl_file_path);
//这里我们的核心是怎样把tpl转化为php文件
$pattern=array(
//1.\{ - 转义{ 左括号 2.\s* - 代表一个或多个空格 3.\$ - 转义$符号 4.\} - 转义 } 右括号
'/\{\s*\$([a-zA-Z][a-zA-Z0-9]*)\s*\}/i'
);
$replace=array(
'<?php echo $this->tpl_vars["${1}"] ?>'
);
//将类似 {$title}替换为<?php echo $this->tpl_vars["title"] ? >,返回替换后的字符串
$new_str=preg_replace($pattern,$replace,$tpl_file_content);
//编译文件的生成:将正则替换后的模版文件中的内容写入到编译文件
file_put_contents($complie_file_path,$new_str);
}
//如果存在编译文件并且模版文件没有改动,则直接引入编译文件
include $complie_file_path;
}
}
?>
注意:对象也可以作为模版变量
案例:
php文件:
$smarty=new Smarty();
$var1=“简单字符串”;
$var2=new 对象名();
$var3=array(内容列表..);
$smarty->assign(“var1”,$var1);
$smarty->assign(“var2”,$var2);
$smarty->assign(“var3”,$var3);
模版文件:
取出 var1 对应的值: {$var1}<br/>
取出 var2 对应的值: {$var2->属性或者方法}
取出 var3 对应的值: {$var3[下标]}
<?php
class MyMiniSmarty{
//模版文件的存放路径
var $template_dir="./templates/";
//编译文件的存放路径 ,编译文件的名称格式暂定为:com_对应的tpl.php
var $complie_dir="./templates_c";
//模版变量数组:存放所有模版变量的值
var $tpl_vars=array();
//这里我们主要模拟两个方法
//参数1-模版变量 参数2:模版变量的值
function assign($tpl_var,$val=null){
if($tpl_var!=''){
$this->tpl_vars[$tpl_var]=$var;
}
}
//这里编写display
//参数1-要显示的模版文件名
function display($tpl_file){
//模版文件的路径
$tpl_file_path=$this->template_dir.$tpl_file;
//编译文件的命名及路径
$complie_file_path=$this->complie_dir."com_".$tpl_file.".php";
//判断当前模版文件是否存在
if(!file_exists($tpl_file_path)){
//如果当前模版文件不存在,则返回false
return false;
}
//查看是否有编译文件,如果没有编译文件,或者说模版文件的修改时间大于编译文件的生成时间,则需要重新编译
if(!file_exists($comlie_file_path) ||filemtime($tpl_file_path)>filemtime($complie_file_path)){
//获取模版文件的内容
$tpl_file_content=file_get_contents($tpl_file_path);
//这里我们的核心是怎样把tpl转化为php文件
$pattern=array(
//1.\{ - 转义{ 左括号 2.\s* - 代表一个或多个空格 3.\$ - 转义$符号 4.\} - 转义 } 右括号
'/\{\s*\$([a-zA-Z][a-zA-Z0-9]*)\s*\}/i'
);
$replace=array(
'<?php echo $this->tpl_vars["${1}"] ?>'
);
//将类似 {$title}替换为<?php echo $this->tpl_vars["title"] ? >,返回替换后的字符串
$new_str=preg_replace($pattern,$replace,$tpl_file_content);
//编译文件的生成:将正则替换后的模版文件中的内容写入到编译文件
file_put_contents($complie_file_path,$new_str);
}
//如果存在编译文件并且模版文件没有改动,则直接引入编译文件
include $complie_file_path;
}
}
?>
注意:对象也可以作为模版变量
案例:
php文件:
$smarty=new Smarty();
$var1=“简单字符串”;
$var2=new 对象名();
$var3=array(内容列表..);
$smarty->assign(“var1”,$var1);
$smarty->assign(“var2”,$var2);
$smarty->assign(“var3”,$var3);
模版文件:
取出 var1 对应的值: {$var1}<br/>
取出 var2 对应的值: {$var2->属性或者方法}
取出 var3 对应的值: {$var3[下标]}
相关文章推荐
- 5.3 基本计算机颜色
- template
- android82 文件下载框架xUtils
- Game of Life
- Linux安装sbt过程详解
- Divide Two Integers 解答
- 分硬币问题
- 设计模式学习笔记--命令模式
- c# 句柄数不断攀升的解决方案
- <编写有效用例>读书笔记3
- [hdu4436 str2int]后缀自动机SAM(或后缀数组SA)
- Leetcode #134 Gas Station
- loginEx
- [hdu4416 Good Article Good sentence]后缀自动机SAM
- Xcode7.0.1编译报错“You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE),”
- [Angular 2] Validation
- Q-3
- 黑马程序员-Java I/O流(二)-File类、Properties类、打印流、序列流(合并流)
- javaCode二维数组
- calmari日志提示OperationalError Permission denied