老師講的優(yōu)化 可以粗淺的改了一下
//模板引擎類--顯示模板
? ? public function display($templateName = 'null', $ext = '.html'){
? ? ? ? $templateName = empty($templateName) ? $this->currentTemp : $templateName;
? ? ? ? //檢測(cè)編譯目錄下是否已經(jīng)存在編譯的目標(biāo)文件,不存在則編譯它
? ? ? ? if(file_exists($this->compileDir.md5($templateName).$ext)){
? ? ? ? ? ? // 獲取源文件的最后修改時(shí)間
? ? ? ? ? ? $getSourceTemplateTime = filemtime($this->templateDir.$templateName.$ext);
? ? ? ? ? ? // 獲去編譯文件的最后編譯時(shí)間
? ? ? ? ? ? $getCompiledTemplateTime = filemtime($this->compileDir.md5($templateName).$ext);
? ? ? ? ? ? //源文件是不是比編譯后的文件修改時(shí)間更遲,如果是說明源文件已經(jīng)被改動(dòng)過了,需重新編譯這個(gè)模板;反之,則說明編譯后沒有再修改過模板源文件,那么就直接加載已經(jīng)編譯好的模板源文件
? ? ? ? ? ? if($getSourceTemplateTime>$getCompiledTemplateTime){
? ? ? ? ? ? ? ? //獲取到模板的源文件
? ? ? ? ? ? ? ? $this->getSourceTemplate($templateName);
? ? ? ? ? ? ? ? //編譯模板文件,不傳名稱,使用默認(rèn)的index
? ? ? ? ? ? ? ? $this->compileTemplacte();
? ? ? ? ? ? }
? ? ? ? }else {
? ? ? ? ? ? //獲取到模板的源文件
? ? ? ? ? ? $this->getSourceTemplate($templateName);
? ? ? ? ? ? //編譯模板文件,不傳名稱,使用默認(rèn)的index
? ? ? ? ? ? $this->compileTemplacte();
? ? ? ? }
? ? ? ? //將代碼輸出,用include_once來(lái)包含編譯后的模板文件
? ? ? ? include_once $this->compileDir.md5($templateName).$ext;
? ? }
2017-12-29
模板源文件也要去獲取最后修改時(shí)間