View.class.php
/**
* 加載模板和頁面輸出 可以返回輸出內容
* @access public
* @param string $templateFile 模板文件名
* @param string $charset 模板輸出字符集
* @param string $contentType 輸出類型
* @param string $content 模板輸出內容
* @param string $prefix 模板緩存前綴
* @return mixed
*/
public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
G('viewStartTime');
// 視圖開始標簽
Hook::listen('view_begin',$templateFile);
// 解析并獲取模板內容
$content = $this->fetch($templateFile,$content,$prefix);
// 輸出模板內容
$this->render($content,$charset,$contentType);
// 視圖結束標簽
Hook::listen('view_end');
}
/**
* 解析和獲取模板內容 用于輸出
* @access public
* @param string $templateFile 模板文件名
* @param string $content 模板輸出內容
* @param string $prefix 模板緩存前綴
* @return string
*/
public function fetch($templateFile='',$content='',$prefix='') {
if(empty($content)) {
$templateFile = $this->parseTemplate($templateFile);
// 模板文件不存在直接返回
if(!is_file($templateFile)) E(L('_TEMPLATE_NOT_EXIST_').':'.$templateFile);
}
// 頁面緩存
ob_start();
ob_implicit_flush(0);
if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板
$_content = $content;
// 模板陣列變量分解成為獨立變量
extract($this->tVar, EXTR_OVERWRITE);
// 直接載入PHP模板
empty($_content)?include $templateFile:eval('?>'.$_content);
}else{
// 視圖解析標簽
$params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix);
Hook::listen('view_parse',$params);
}
// 獲取并清空緩存
$content = ob_get_clean();
// 內容過濾標簽
Hook::listen('view_filter',$content);
// 輸出模板文件
return $content;
}
接下來和《
ThinkPHP生成HTML靜態文件和模板緩存的過程
》的過程一樣,只不過在用到WriteHtmlCacheBehavior類
WriteHtmlCacheBehavior.class.php
/**
* 系統行為擴展:靜態緩存寫入
*/
class WriteHtmlCacheBehavior {
// 行為擴展的執行入口必須是run
public function run(&$content){
if(C('HTML_CACHE_ON') && defined('HTML_FILE_NAME')) {
//靜態文件寫入
Storage::put(HTML_FILE_NAME , $content,'html');
}
}
}
這里讀取了,配置中
HTML_CACHE_ON來判斷是否要生成靜態HTML文件