thinkPHP3.0框架实现模板保存到数据库的方法(3)

5.添加自己解析模板内容的类TemplateEy.class.php(这个放在thinkphp\Extend\Driver\Template目录下面)
只是将系统默认的ThinkTemplate.class.php类修改了fetch方法修改代码如下:

// 加载模板 public function fetch($templateFile,$templateContent,$templateVar) { $this->tVar = $templateVar; if($templateContent && !$templateFile) { //不缓存 if(C('LAYOUT_ON')) { if(false !== strpos($templateContent,'{__NOLAYOUT__}')) { // 可以单独定义不使用布局 $templateContent = str_replace('{__NOLAYOUT__}','',$templateContent); }else{ // 替换布局的主体内容 $layoutFile = THEME_PATH.C('LAYOUT_NAME').$this->config['template_suffix']; $templateContent = str_replace($this->config['layout_item'],$templateContent,file_get_contents($layoutFile)); } } //编译模板内容 $templateContent = $this->compiler($templateContent); extract($templateVar, EXTR_OVERWRITE); echo $templateContent; } else { $templateCacheFile = $this->loadTemplate($templateFile,$templateContent); // 模板阵列变量分解成为独立变量 extract($templateVar, EXTR_OVERWRITE); //载入模版缓存文件 include $templateCacheFile; } }

6.调用如果数据库中模板的内容不存在那么我们还是去读数据库中的内容:

if( array_key_exists( $display_mode, $params['tpl'] ) && strlen($params['tpl'][$display_mode]) > 0 ){ return $this->fetch("Ext:New:Frontend:show",$params['tpl'][$display_mode]); }else{ return $this->fetch("Ext:New:Frontend:show"); }

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3639d842d2fc3de93444007745648c77.html