国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php模板引擎技術簡單實現

2024-05-04 23:43:59
字體:
來源:轉載
供稿:網友
這篇文章主要為大家詳細介紹了php模板引擎技術簡單實現,感興趣的小伙伴們可以參考一下
 

用了smarty,tp過后,也想了解了解其模板技術是怎么實現,于是寫一個簡單的模板類,大致就是讀取模板文件->替換模板文件的內容->保存或者靜態化 

tpl.class.php主要解析

  assign 方法實現     

/**     * 模板賦值操作     * @param mixed $tpl_var 如果是字符串,就作為數組索引,如果是數組,就循環賦值     * @param mixed $tpl_value 當$tpl_var為string時的值,默認為 null     */    public function assign($tpl_var,$tpl_value=null){      if(is_array($tpl_var) && count($tpl_var) > 0){        foreach ($tpl_var as $k => $v) {          $this->tpl_vars[$k] = $v;        }      }elseif($tpl_var){        $this->tpl_vars[$tpl_var] = $tpl_value;      }    }

fetch 方法實現

     /**      * 生成編譯文件      * @param string $tplFile 模板路徑      * @param string $comFile 編譯路徑      * @return string     */    private function fetch($tplFile,$comFile){      //判斷編譯文件是否需要重新生成(編譯文件是否存在或者模板文件修改時間大于編譯文件的修改時間)      if(!file_exists($comFile) || filemtime($tplFile) > filemtime($comFile)){        //編譯,此處也可以使用ob_start()進行靜態化        $content = $this->tplReplace(file_get_contents($tplFile));        file_put_contents($comFile, $content);      }    }  

簡單編譯方法:按照規則進行正則替換

 /**     * 編譯文件     * @param string $content 待編譯的內容     * @return string     */    private function tplReplace($content){      //轉義左右定界符 正則表達式字符      $left = preg_quote($this->left_delimiter,'/');      $right = preg_quote($this->right_delimiter,'/');      //簡單模擬編譯 變量      $pattern = array(          //例如{$test}          '/'.$left.'/$([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff]*)'.$right.'/i'        );      $replace = array(          '<?php echo $this->tpl_vars[/'${1}/']; ?>'        );      //正則處理      return preg_replace($pattern, $replace, $content);    }

display = fetch+echo

 /**     * 輸出內容     * @param string $fileName 模板文件名     */    public function display($fileName){      //模板路徑      $tplFile = $this->template_dir.'/'.$fileName;      //判斷模板是否存在      if(!file_exists($tplFile)){        $this->errorMessage = '模板文件不存在';        return false;      }      //編譯后的文件      $comFile = $this->compile_dir.'/'.md5($fileName).'.php';      $this->fetch($tplFile,$comFile);             include $comFile;    }

其他屬性

 //模板文件存放位置    private $template_dir = 'templates';     //編譯文件存放位置    private $compile_dir = 'compiles';    //左定界符    private $left_delimiter = '{';    //右定界符     private $right_delimiter = '}';     //內部臨時變量,存儲用戶賦值    private $tpl_vars = array();    //錯誤信息    private $errorMessage = '';    /**     * 修改類屬性的值     * @param array $configs 需要修改的相關屬性及值     * @return bool     */    public function setConfigs(array $configs){      if(count($configs) > 0){        foreach ($configs as $k => $v) {          if(isset($this->$k))            $this->$k = $v;        }        return true;      }      return false;    }

測試

模板文件 testTpl.html

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>test_tpl_demo</title></head><body>  {$name}:{$age}:{$message}</body></html>運行文件 test_tpl.php<?php  require 'Tpl.class.php';    $tpl = new Tpl();  $tplarr = array(      'name'=>'waited',      'age'=>'100'    );  $tpl->assign($tplarr);  $tpl->assign('message','this is a demo');  $tpl->display('testTpl.html');?>

輸出:waited:100:this is a demo

生成編譯文件:972fa4d270e295005c36c1dbc7e6a56c.php

以上就是本文的全部內容,希望對大家的學習有所幫助。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五台县| 荃湾区| 水富县| 溧阳市| 霍州市| 河南省| 麻阳| 白玉县| 婺源县| 扶余县| 利辛县| 衡南县| 碌曲县| 开鲁县| 时尚| 本溪| 盖州市| 平罗县| 赫章县| 湘潭县| 秦皇岛市| 寿阳县| 旺苍县| 黑龙江省| 雷波县| 无极县| 中宁县| 车致| 确山县| 綦江县| 开江县| 留坝县| 黄陵县| 巨野县| 涡阳县| 班戈县| 包头市| 蚌埠市| 南部县| 华坪县| 洱源县|