Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1564441
  • 博文数量: 1481
  • 博客积分: 26784
  • 博客等级: 上将
  • 技术积分: 17045
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-12 09:22
文章分类

全部博文(1481)

文章存档

2014年(10)

2013年(353)

2012年(700)

2011年(418)

分类: 系统运维

2012-08-03 09:21:45

MyMiniSmarty.class.php

class MyMiniSmarty{
//我们的模板文件的路径
var $template_dir = "./templates/";
var $complie_dir = "./templates_c/";
//这里我们还需要指定一个模板文件被替换后的文件 格式com_对应的tpl.php
//存放变量值
var $tpl_vars=array();

//这里我们主要模拟两个方法
function assign($tpl_var, $val=null){
if($tpl_var!=''){
$this->tpl_vars[$tpl_var]=$val;
}
}

//这里编写display
function display($tpl_file){

//读取这个模板文件->替换可以运行的一个php(编译后的文件)
$tpl_file_path=$this->template_dir.$tpl_file;
// echo $tpl_file_path;
$complie_file_path=$this->complie_dir."com_".$tpl_file.".php";
// echo $complie_file_path;
//判断文件存在否。
if(!file_exists($tpl_file_path)){
echo "asdfasf"; //只要文件存在就不执行,搁着
return false;
}
// 有没有必要每次都去生成一个编译后文件
if(!file_exists($complie_file_path) || filemtime($tpl_file_path)>filemtime($complie_file_path)){
//这里使用了缓存机制

$fpl_file_con=file_get_contents($tpl_file_path);
//这里我们的核心怎样把tpl转成php文件
//下面可以封装成一个函数
$pattern=array(
'/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i'
);
$replace=array(
'tpl_vars["${1}"] ?>'
);

$new_str=preg_replace($pattern, $replace, $fpl_file_con);
file_put_contents("./templates_c/com_intro.tpl.php",$new_str);
}
//引入编译后的文件
include $complie_file_path;
}
}
?>

intro.php

{$title}

我的第一套模板


{$content}



intro.tpl

{$title}

我的第一套模板


{$content}

原文地址:

阅读(243) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~