自己動手寫一個簡單的php模板引擎
模板引擎中最核心的思想是:將模板中的變數編譯為php的變數進行輸出。
例如:demo.tpl
{$data}{$title}
那麼模板引擎就要將{$data} {$title} 編譯為
要實現這個功能使用正則替換就可以了:
$content = '{$data}{$title}';$pattern = "/\{\\$([a-zA-Z_][a-zA-Z0-9_]*)\}/";$content = preg_replace($pattern,'tmpValue["$1"] ?>',$content);echo $content; //
這就是php模板引擎的核心功能了。下面是我寫的一個簡單的php模板引擎
首先是tempTool.class.php 它的作用的提供模板引擎需要用到的一些小工具
error[$k] = $v; }else { exit('tempTool.class.php的error方法收到了不確定的參數錯誤!'); } } /** * 擷取錯誤資訊 **/ public function getError() { foreach($this->error as $k=>$v) { echo $k.$v.'
'; } }}?>
然後是template.class.php 它的作用是提供模板引擎應該有的功能
'template/',// 模板檔案目錄'cmpDir'=>'compile/',// 編譯檔案目錄'cacheDir'=>'cache/',// 快取檔案目錄'tmpSuffix'=>'.tpl',// 模板檔案尾碼'cacheSuffix'=>'.html',// 快取檔案尾碼'caching'=>false// 是否開啟緩衝);private $tmpFile;// 模板檔案private $cmpFile;// 編譯檔案private $cacheFile;// 快取檔案private $tmpValue = array();// 變數值棧public function __construct($config = null){// 同步配置if(is_array($config)) {$this->config = array_merge($this->config,$config);}// 檢查模板編譯緩衝目錄是否存在不存在建立if( !$this->checkDir($this->config['tmpDir']) || !$this->checkDir($this->config['cmpDir']) || !$this->checkDir($this->config['cacheDir']) ) {exit;}}/** * 檢查目錄是否存在不存在自動建立 **/private function checkDir($dir){if(!is_dir($dir)) {if(!mkdir($dir)) {$this->errorLog[] = array('建立檔案夾失敗:'=>$dir);return false;}}return true;}/** * 像模板中分配變數 * **/public function assign($k,$v){if(!empty($k) && !empty($v)) {$this->tmpValue[$k] = $v;}else {$this->error('分配變數失敗:','分配到模板中變數的key或者value為空白!');}}/** * 編譯檔案 **/public function display($tmpFile){// 擷取模板檔案$tmpFile = $this->config['tmpDir'].$tmpFile.$this->config['tmpSuffix'];// 擷取編譯檔案$cmpFile = $this->config['cmpDir'].md5($tmpFile.'compile').'.php';if(!file_exists($tmpFile)) {$this->error('模板檔案不存在:',$tmpFile.'不存在');}// 當編譯檔案不存在或者是模板檔案被修改過了才重新編譯if(!file_exists($cmpFile) || filemtime($cmpFile) < filemtime($tmpFile)) {include_once "compile.class.php";$cmp = new compile();$cmp->cmp($tmpFile,$cmpFile);}// 是否開啟緩衝if($this->config['caching']) {// 擷取快取檔案$cacheFile = $this->config['cacheDir'].md5($tmpFile.'cache').$this->config['cacheSuffix'];// 當快取檔案不存在或者是模板檔案被修改過重建快取檔案if(!file_exists($cacheFile) || filemtime($cacheFile) < filemtime($tmpFile)) {ob_start();include_once $cmpFile;$content = ob_get_clean();if(!file_put_contents($cacheFile,$content)){$this->error('編譯檔案產生失敗:',$cacheFile);}}//載入快取檔案 include $cacheFile;}else {// 載入編譯檔案include_once $cmpFile;}}}
compile.class.php 編譯類將模板檔案編譯為php檔案
error("檔案讀取失敗:",$tmpFile);}$pattern = "/\{\\$([a-zA-Z_][a-zA-Z0-9_]*)\}/";$this->content = preg_replace($pattern,'tmpValue["$1"] ?>',$content);$this->parse($cmpFile);}/** * 將編譯好的內容寫入檔案 **/public function parse($cmpFile){if(!file_put_contents($cmpFile,$this->content)){$this->error('檔案寫入失敗:',$cmpFile);}}}?>
下面進行一下測試:
1. 建立一個 template 檔案夾 在裡面寫 一個模板 demo.tpl
{$data}{$title}2.建立一個demo.php
true);include_once "template.class.php";$tmp = new template($config);$tmp->assign('data','ccc');$tmp->assign('title','這時測試案例');$tmp->display('demo');
結果輸出
ccc這時測試案例
可以看一下cache目錄和 compile目錄下 會產生一個快取檔案 和一個編譯檔案
這時就成功了