我看源代码里有个加载layout的代码:
/**
* 加载主模板并缓存
* @access public
* @param string $tmplTemplateFile 模板文件
* @param string $prefix 模板标识前缀
* @return string
* @throws ThinkExecption
*/
public function loadTemplate ($tmplTemplateFile,$prefix='') {
if(is_file($tmplTemplateFile)) {
$this->templateFile = $tmplTemplateFile;
// 读取模板文件内容
$tmplContent = file_get_contents($tmplTemplateFile);
}else{
$tmplContent = $tmplTemplateFile;
}
// 根据模版文件名定位缓存文件
$tmplCacheFile = $this->config['cache_path'].$prefix.md5($tmplTemplateFile).$this->config['cache_suffix'];
// 判断是否启用布局
if(C('LAYOUT_ON')) {
if(false !== strpos($tmplContent,'{__NOLAYOUT__}')) { // 可以单独定义不使用布局
$tmplContent = str_replace('{__NOLAYOUT__}','',$tmplContent);
}else{ // 替换布局的主体内容
$layoutFile = THEME_PATH.C('LAYOUT_NAME').$this->config['template_suffix'];
$tmplContent = str_replace($this->config['layout_item'],$tmplContent,file_get_contents($layoutFile));
}
}
// 编译模板内容
$tmplContent = $this->compiler($tmplContent);
// 检测模板目录
$dir = dirname($tmplCacheFile);
if(!is_dir($dir))
mkdir($dir,0755,true);
//重写Cache文件
if( false === file_put_contents($tmplCacheFile,trim($tmplContent)))
throw_exception(L('_CACHE_WRITE_ERROR_').':'.$tmplCacheFile);
return $tmplCacheFile;
}这个代码就是加载layout的:file_get_contents($layoutFile),我想问下,处理layout为什么这么简单粗暴?为了实现layout里的变量替换,我只能在layout里写PHP代码了......
最佳答案