一个自己写的PHP模板引擎
這是一個自己寫的編譯型的模板引擎(不包括緩存部分)貼上代碼。有問題可以隨時跟帖。
單文件版:JTemplate.class.php
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | <!--?php /** ?* @author Jiawei ?* @copyright Jiawei ?* @Completed in 2012-6-29 0:23 ?* JTemplate V1.0Beta ?*/ class JTemplate{ ????//通過assign函數傳入的變量臨時存放數組 ????private $templateVar = array(); ????//模板目錄 ????private $templateDir = ''; ????//編譯目錄 ????private $templateCompileDir = ''; ????/** ?????* 構造函數 ?????* @param string $templateDir 模板目錄 ?????* @param string $templateComplieDir 模板編譯目錄 ?????*/ ????public function __construct($templateDir,$templateComplieDir){ ????????$this--->templateDir = $templateDir; ????????$this->templateCompileDir = $templateComplieDir; ????} ????/** ?????* 顯示模板方法 ?????* @param string $fileName 模板文件名 ?????*/ ????public function display($fileName){ ????????if(file_exists($this->templateDir.'/'.$fileName)){ ????????????$compileFileName = $this->templateCompileDir.'/'.$this->file_safe_name($fileName).'.php'; ????????????if(!file_exists($compileFileName) || filemtime($compileFileName)< filemtime($this->templateDir.'/'.$fileName)){ ????????????????$this->del_old_file($fileName); ????????????????$this->compile($fileName); ????????????} ????????????extract($this->templateVar); ????????????include $compileFileName; ????????}else{ ????????????$this->error('Sorry,the template file '.$fileName.' does not exist!!'); ????????} ????} ????/** ?????* 獲取編譯文件名方法 ?????* @param string $fileName 模板文件名 ?????*/ ????public function get_compile_file($fileName){ ????????$compileFile = explode('.',$fileName); ????????unset($compileFile[count($compileFile)-1]); ????????return implode('.',$compileFile); ????} ????/** ?????* 編譯方法 ?????* @param string $fileName 模板文件名 ?????*/ ????public function compile($fileName){ ????????$fileHandle = @fopen($this->templateDir.'/'.$fileName, 'r'); ????????while(!feof($fileHandle)){ ????????????$fileContent = fread($fileHandle,1024); ????????} ????????fclose($fileHandle); ????????$fileContent = $this->template_replace($fileContent); ????????//$compileFile = $this->get_compile_file($fileName); ????????$fileHandle = @fopen($this->templateCompileDir.'/'.$this->file_safe_name($fileName).'.php','w'); ????????if($fileHandle){ ????????????fwrite($fileHandle, $fileContent); ????????????fclose($fileHandle); ????????}else{ ????????????$this->error('Sorry,Compile dir can not write!'); ????????} ????} ????/** ?????* 模板傳值方法 ?????* @param string $valueName 模板中使用的變量名 ?????* @param all $value 變量值 ?????*/ ????public function assign($valueName,$value){ ????????$this->templateVar[$valueName] = $value; ????} ???????? ????/** ?????* 模板正則替換方法 ?????* @param string $content 替換內容 ?????* @return string 替換過后的內容 ?????*/ ????public function template_replace($content){ ????????$orginArray = array( ????????????'/{C}<!--loop\s+\$(\w+)\s+\$(\w+)-->/s', ????????????'/{C}<!--loop\s+\$(\w+)\s+\$(\w+)\s+\$(\w+)-->/s', ????????????'/{C}<!--elseloop-->(.+?){C}<!--endloop-->/s', ????????????'/{C}<!--endloop-->/s', ????????????'/{C}<!--if\s+\((.+?)\)-->/s', ????????????'/{C}<!--endif-->/s', ????????????'/{C}<!--elseif\s+\((.+?)\)-->/s', ????????????'/{C}<!--else-->/s', ????????????'/\{P:(.+?)\:}/s', ????????????'/\{C:(\w+)\}/s', ????????????'/\{I:(.+?)\}/s', ????????????'/\{F:(.+?)\}/s', ????????????'/\{EF:(.+?)\}/s', ????????????'/\{([a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s', ????????); ???????????? ????????$changeArray = array( ????????????'<!--?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2){$countLoop++;?-->', ????????????'<!--?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2=-->$$3){$countLoop++;?>', ????????????'<!--?php }if(!empty($countLoop))$countLoop--;}else{?-->$1<!--?php }?-->', ????????????'<!--?php }if(!empty($countLoop))$countLoop--;}?-->', ????????????'<!--?php if($1){?-->', ????????????'<!--?php }?-->', ????????????'<!--?php }elseif($1){?-->', ????????????'<!--?php }else{?-->', ????????????'<!--?php $1?-->', ????????????'<!--?php echo $1;?-->', ????????????'<!--?php include_once "'.$this--->templateDir.'/$1";?>', ????????????'<!--?php $1;?-->', ????????????'<!--?php echo $1;?-->', ????????????'<!--?php echo $$1;?-->', ????????); ????????return $repContent=preg_replace($orginArray,$changeArray,$content); ????} ????/** ?????* 刪除舊文件 ?????* @param string $fileName 模板文件名 ?????*/ ????public function del_old_file($fileName){ ????????$compileFile = $this->get_compile_file($fileName); ????????$files = glob($this->templateCompileDir.'/'.$compileFile.'*.php'); ????????// print_r($files); ????????if($files){ ????????????@unlink($files[0]); ????????} ????} ????/** ?????* 編譯文件名安全處理方法 ?????* @param string $fileName 傳入模板文件名 ?????* @param string 返回編譯文件名 ?????*/ ????public function file_safe_name($fileName){ ????????$compileFile = $this->get_compile_file($fileName); ????????return $compileFile.filemtime($this->templateDir.'/'.$fileName); ????} ???????? ????/** ?????* 錯誤輸出函數 ?????* @param string $content 錯誤輸出信息 ?????*/ ????public function error($content){ ????????$stringHtml = ' |
'; $stringHtml .= ''; $stringHtml .= $content; $stringHtml .= ''; $stringHtml .= ' '; exit($stringHtml); } } ?>
?
模板引擎使用方法:
首先載入模板引擎核心文件JTemplate.class.php
include_once 'JTemplate/JTemplate.class.php';
實例化模板引擎:
$template = new JTemplate(模板目錄,編譯目錄);
模板引擎方法:
1.assign方法用來將值傳入模板中
$template->assign('模板引擎中用的變量名');
2.display顯示模板文件方法:
$template->display('模板文件');
模板語法使用方法:
輸出變量
{變量名} //不帶$符號
判斷語句
內容1內容2內容3//可拆分使用
遍歷數組
循環內容
相當于
foreach($a as $v){}
遍歷數組2:
循環內容當數組為空或傳入變量不為數組的時候輸出的內容
遍歷中的計數器
要在遍歷中使用計數器可以使用變量{countLoop}來計算當前循環的次數如果在if中使用請使用$countLoop
{countLoop} //在循環內輸出當前循環的次數。在循環外使用輸出最近的一次循環循環了多少次
//if語句中這樣使用
輸出常量
{C:常量名}
載入文件
{I:載入文件的路徑及文件名及后綴}
執行函數不輸出
{F:函數名(參數,參數)}
執行函數并輸出函數返回的結果
{EF:函數名(參數,參數)}
執行PHP原生語句
{P:PHP語句:}
總結
以上是生活随笔為你收集整理的一个自己写的PHP模板引擎的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用js写一个模板引擎
- 下一篇: 动态规划算法php,php算法学习之动态