本文實例講述了thinkPHP3.0框架實現模板保存到數據庫的方法。分享給大家供大家參考,具體如下:
在開發cms的時候用到如果將模板文件存入到數據庫并顯示到頁面中
由于thinkphp3.0都是直接從模板文件中讀取再解析的那么對于模板存入數據庫中就只有自己開發了,還有thinkphp3.0中有mode的功能我們可以定義自己的mode這樣就可以達到目的了,那么如何來擴展自己的mode呢?如下:
1.在你的入口文件中輸入
define('MODE_NAME','Ey');
其中"Ey"就是你自己擴展的mode名稱了,請在你的thinkphp/Extend/Mode文件下面創建Ey文件夾
2.在Ey目錄中修改
添加tags.php文件內容如下:
return array( 'app_init'=>array( ), 'app_begin'=>array( 'ReadHtmlCache', // 讀取靜態緩存 ), 'route_check'=>array( 'CheckRoute', // 路由檢測 ), 'app_end'=>array(), 'path_info'=>array(), 'action_begin'=>array(), 'action_end'=>array(), 'view_begin'=>array(), 'view_template'=>array( 'ExtensionTemplate', // 自動定位模板文件(手動添加) ), 'view_content'=>array( 'ParseContent'//(手動添加) ), 'view_filter'=>array( 'ContentReplace', // 模板輸出替換 'TokenBuild', // 表單令牌 'WriteHtmlCache', // 寫入靜態緩存 'ShowRuntime', // 運行時間顯示 ), 'view_end'=>array( 'ShowPageTrace', // 頁面Trace顯示 ), );
該文件中后面的注釋中添加手動添加了為我的修改,只是修改thinkphp中默認的tags中查找模板和解析模板的行為
將系統默認的action和view類復制到Ey的目錄中(由于解析內容,所以要修改action和view類),修改action.class.php中的fetch方法:
protected function fetch($templateFile='',$templateContent='' ){ return $this->view->fetch($templateFile,$templateContent); }
view.class.php文件中的修改為:
public function fetch($templateFile='',$templateContent = NULL) { $params['templateFile'] = $templateFile; $params['cacheFlag'] = true; if(isset($templateContent)) { $params['templateContent'] = $templateContent; } tag('view_template',$params); // 頁面緩存 ob_start(); ob_implicit_flush(0); if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板 // 模板陣列變量分解成為獨立變量 extract($this->tVar, EXTR_OVERWRITE); // 直接載入PHP模板 include $templateFile; }else{ // 視圖解析標簽 $params = array('var'=>$this->tVar,'content'=>$params['templateContent'],'file'=>$params['templateFile'],'cacheFlag'=>$params['cacheFlag']); tag('view_content',$params); } // 獲取并清空緩存 $content = ob_get_clean(); // 內容過濾標簽 tag('view_filter',$content); // 輸出模板文件 return $content; }
3.擴展自己的查找模板的類(自己擴展的行為tp讓我們放在thinkphp\Extend\Behavior中)
在thinkphp\Extend\Behavior中添加ExtensionTemplateBehavior.class.php類,內容如下:
class ExtensionTemplateBehavior extends Behavior { // 行為擴展的執行入口必須是run public function run(&$params){ if( is_array($params) ){ if( array_key_exists('templateFile', $params) ){ $params = $this->parseTemplateFile($params); }else{ //異常 throw_exception(L('_TEMPLATE_NOT_EXIST_AND_CONTENT_NULL_').'['.$params['templateFile'].']'); } }else{ // 自動定位模板文件 if(!file_exists_case($params)) $params = $this->parseTemplateFile($params); } } private function parseTemplateFile($params) { if( is_array($params) ) { $templateFile = $params['templateFile']; }else{ $templateFile = $params; } if(!isset($params['templateContent'])) { // 是否設置 templateContent 參數 //自動獲取模板文件 if('' == $templateFile){ // 如果模板文件名為空 按照默認規則定位 $templateFile = C('TEMPLATE_NAME'); } elseif(false === strpos($templateFile,C('TMPL_TEMPLATE_SUFFIX'))) { $path = explode(':',$templateFile); //如果是插件 if($path[0] == 'Ext') { $templateFile = str_replace(array('Ext:',$path[1] . ':',$path[2] . ':'),'',$templateFile); $templateFile = SITE_ROOT . '/Ext/extensions/' . strtolower($path[1]) . '/' . $path[2] . '/Tpl/' . $templateFile . C('TMPL_TEMPLATE_SUFFIX'); } else { // 解析規則為 模板主題:模塊:操作 不支持 跨項目和跨分組調用 $action = array_pop($path); $module = !empty($path)?array_pop($path):MODULE_NAME; if(!empty($path)) {// 設置模板主題 $path = dirname(THEME_PATH).'/'.array_pop($path).'/'; }else{ $path = THEME_PATH; } $depr = defined('GROUP_NAME')?C('TMPL_FILE_DEPR'):'/'; $templateFile = $path.$module.$depr.$action.C('TMPL_TEMPLATE_SUFFIX'); } } } else { if('' == $templateFile){ $depr = defined('GROUP_NAME')?C('TMPL_FILE_DEPR'):'/'; $params['cacheFlag'] = false; } else { $path = explode(':',$templateFile); //如果是插件 if($path[0] == 'Ext') { $templateFile = str_replace(array('Ext:',$path[1] . ':',$path[2] . ':'),'',$templateFile); $templateFile = SITE_ROOT . '/Ext/extensions/' . strtolower($path[1]) . '/' . $path[2] . '/Tpl/' . $templateFile . C('TMPL_TEMPLATE_SUFFIX'); } else { // 解析規則為 模板主題:模塊:操作 不支持 跨項目和跨分組調用 $action = array_pop($path); $module = !empty($path)?array_pop($path):MODULE_NAME; if(!empty($path)) {// 設置模板主題 $path = dirname(THEME_PATH).'/'.array_pop($path).'/'; }else{ $path = THEME_PATH; } $depr = defined('GROUP_NAME')?C('TMPL_FILE_DEPR'):'/'; $templateFile = $path.$module.$depr.$action.C('TMPL_TEMPLATE_SUFFIX'); } } } if( is_array($params) ){ $params['templateFile'] = $templateFile; return $params; }else{ if(!file_exists_case($templateFile)) throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']'); return $templateFile; } } }
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。