我自己写了个模板替换的规则,但是在输出内容的时候不知道怎么回事$template = dirname(__FILE__).'\template\default\\'; //模板路径
$cache = dirname(__FILE__).'\cache\\'; //缓存路径
class template
{
public $pattern=array('/\{\$([a-zA-Z].*[0-9]*)\}/',    //匹配普通变量
                      '/\{template\s([a-zA-Z].*[0-9]*)\}/', //匹配文件包含
  '/\{if\s(.*?)\}/'); //匹配if
public $replace=array('<?php echo ${1} ?>',
                      '<?php include"${1}" ?>',
  '<?php if($(1) ?>,');
public $sfopen;
public $sfile;
public $write;
public $array_var=array();
function openfile($sfile) // 模板替换输出
{
global $cache;
global $template;
$template=$template.$sfile;
if(file_exists($template))
{
$sfopen=file_get_contents($template);
$this->write=preg_replace($this->pattern,$this->replace,$sfopen);
    file_put_contents($cache.$sfile,$this->write1);  
}
}
这个是模板文件内容{template header.html}
<div id="index_1">
{foreach $links as $links1}
{$links1}
{/foreach}
</div>
{template footer.html}替换后输出的内容<?php include"header.html" ?>
<div id="index_1">
{foreach $links as $links1}
<?php echo links1 ?>
{/foreach}
</div>
<?php include"footer.html" ?>
我想问下 怎么让 上面2个include 语句执行后了在保存在缓存文件夹里面啊????

解决方案 »

  1.   

    我写了一个,你可以借鉴
    <?PHP
    /**
    * Copyright (c) 2010-10-13 F.seven ([email protected])
    * 注意事项:
    * 如果该类的实例化页面存在多个使用数字作为索引的数组,为了防止值被重写,请先将多个数组合并,然后再用set方法初始化模板变量
    * 如果该类在实例化时设置了缓存时间,则缓存时间在当前会话内一直有效无法更改,但是缓存id可以使用setid方法变更
    */Class Template{
    var $vars; // 用來保存所有的模板變量
    var $debug; // 是否开启调试模式
    var $cheid; // 緩存id
    var $expire;
    var $cached;
    var $tplpath; // 模板文件存放的路徑,須以/結尾
    var $chepath; // 緩存文件存放的位置,須以/結尾 /* 構造函數
    * @參數 (string) $cheid 自定義的緩存id(针对用过程开发的方式,防止同一个文件不同的过程仅缓存一处)
    * @參數 (int) $expire 緩存的生存周期(秒)
    * @參數 (string)$tplpath 模板文件存放的路徑
    * @參數 (string)$chepath 緩存文件存放的路徑
    * @返回 (null)
    */
    Public Function Template($cheid="",$expire=3600,$tplpath="./tpl/",$chepath="cache/"){
    $this->vars = array();
    $this->debug = YJ_Debug; //是否開啟調試
    $this->expire = $expire;
    $this->tplpath = $tplpath;
    $this->chepath = $chepath; //根据逻辑层文件的GET和POST值以及用户传递的缓存id产生新的cheid,可防止产生無用的缓存文件
    //增加傳遞的$cheid的值,防止同一個文件包含不同的過程時誤采用同一個緩存文件
    //$this->cheid = $chepath.$cheid.Md5_file(Basename($_SERVER["PHP_SELF"]));
    $this->cheid = $chepath.sha1($cheid.$_SERVER['PHP_SELF'].serialize($_GET).serialize($_POST)).".cache";
    } // 重新設置緩存id
    Public Function setid($cheid){
    $this->cheid = $this->chepath.sha1($cheid.$_SERVER['PHP_SELF'].serialize($_GET).serialize($_POST)).".cache";
    } // 重新設置模板文件的存放路徑
    Public Function setPath($tplpath){
    $this->tplpath = $tplpath;
    } /*
    * 設置模板所使用的變量
    * 1:如果$vars是數組,則用法為:$Array=array("a"=>1,0=>2);set($Array);
    * @作用 將數組內的元素拆分,鍵名作為模板的變量名,鍵值作為變量的值
    *
    * 2:如果$vars是字符串,則用法為:set("varName","varValue");
    * @作用 產生名稱為varName的變量,變量的值是varValue
    */
    Public Function set($vars,$value=""){
    IF(Is_array($vars)){
    $this->vars = Array_merge($this->vars, $vars);
    $this->vars = $this->FormatKey($this->vars);
    }Else{
    $this->vars[$vars] = $value;
    }
    } /*
    * 重新格式化數組,将数字索引的数组键名修改成符合变量所要求的标准(即在数字键名前加_)
    * @例子 Array("a"=>"a","b",3=>"c")转换之后是Array("a"=>"a","_0"=>"b","_3"=>"c")
    * @參數 (array) 待格式化的数组
    * @返回 (array)
    */
    Private Function FormatKey($arr){
    $_keys=Array_keys($arr);
    Foreach($_keys as $key=>$value){
    IF(Is_numeric($value)){
    $_keys[$key]="_".$value;
    }
    }
    Return Array_combine($_keys,$arr);
    } /*
    * 根據cheid判斷當前的緩存是否有效
    * @返回 (bool)
    */
    Private Function Is_cached(){
    IF($this->cached){Return True;}
    IF(!$this->cheid){Return False;}
    IF(!File_exists($this->cheid)){Return False;}
    IF(!($mtime = Filemtime($this->cheid))){Return False;} // 緩存是否已過期?
    IF(($mtime + $this->expire) < time()){
    @Unlink($this->cheid);
    Return False;
    }Else{
    $this->cached = True;
    Return True;
    }
    } /*
    * 核心解析引擎,解析并返回已包含内容的缓存文件
    * @參數 (string) 模板文件名
    * @返回 (string)
    */
    Public Function Exec($file){
    IF(!File_Exists($this->tplpath.$file)){Return False;}
    Extract($this->vars); // 提取變量到當前命名空間
    OB_start(); // 開始輸出緩沖
    OB_clean(); // 清理原有內容
    Include($this->tplpath . $file); // 調用模板文件
    $contents = ob_get_contents(); // 取得緩存區內容
    ob_end_clean(); // 結束緩沖區
    Return $contents; // 返回模板文件執行結果后的內容
    } /*
    * 根據cheid对内容进行缓存
    * @參數 (string) $file 模板文件名
    * @返回 (string)
    */
    Public Function Engine($file){
    IF($this->Is_cached()){
    IF($this->debug){Echo "<br><b style='color:blue;'>{读取}</b>{$this->cheid}<br>";}
    $contents = File_Get_Contents($this->cheid);
    }Else{
    IF($this->debug){Echo "<br><b style='color:red;'>{寫入}</b>{$this->cheid}<br>";}
    $contents = $this->Exec($file);
    File_Put_Contents($this->cheid,$contents,LOCK_EX);
    }
    $this->vars=Array();
    Return $contents;
    } /*
    * 常规内容缓存,不需要考虑模板和变量的问题
    * 即,将整个页面执行结果放入缓存文件内,在有效的缓存期内直接读缓存文件并显示
    * 取得整个页面执行结果的方法如下:
    * OB_start();
    * $html = Ob_get_contents();
    * OB_end_clean();
    */
    Public Function Cache($html=""){
    IF($this->Is_cached()){
    IF($this->debug){Echo "<br><b style='color:blue;'>{读取}</b>{$this->cheid}<br>";}
    Return File_Get_Contents($this->cheid);
    }Else{
    //如果沒有緩存,則寫入,并輸出
    IF(StrLen($html)>0){
    IF($this->debug){Echo "<br><b style='color:red;'>{寫入}</b>{$this->cheid}<br>";}
    File_Put_Contents($this->cheid,$html,LOCK_EX) Or Die($this->cheid);
    Echo $html;
    }Else{
    Return False;
    }
    }
    } // 手工刪除緩存文件
    Public Function delCache(){
    IF(!Is_dir($this->chepath)){Return False;}
    $handle = openDir($this->chepath);
    While(($fname = Readdir($handle))!==false){
    IF(Is_file($this->chepath.$fname) And StrToLower(End(Explode(".",$fname)))=="cache"){
    @UnLink($this->chepath.$fname);
    }
    }
    $this->cached = False;
    }
    }
    ?>
      

  2.   

    为什么一定要自己写呢。觉得自己写可能效率上可能会比SMARTY弱。而且不统一。
      

  3.   

    !Smarty吧!
    好的程序员要做的是站在巨人肩上!
    你可以看看Smarty是怎么写的!
    比较费时间!
    但是你读完整个程序,我敢保证你也是小神一尊了!