ThinkPHP 模板解析流程,研究筆記。

來源:互聯網
上載者:User

ThinkPHP 控制器-方法中,通過

$this->display();

來輸出帶有模板的視圖。

那麼就從這個方法入手。

1.Action.class.php  控制器基類

這是控制器基類,在這裡面找到display()方法。

    protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {        $this->view->display($templateFile,$charset,$contentType,$content,$prefix);    }

可以看出,這裡實際上是調用 View視圖類的display()方法。

2.View.class.php 視圖類

public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {        ...        // 解析並擷取模板內容        $content = $this->fetch($templateFile,$content,$prefix);        // 輸出模板內容        $this->render($content,$charset,$contentType);        ...}

關鍵在於fetch()方法和render()方法,其他的不管。

public function fetch($templateFile='',$content='',$prefix='') {        ...//沒有指定模板檔案的話,自動定位模板檔案,省略        // 頁面緩衝        ob_start();        ob_implicit_flush(0);        if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板            // 模板陣列變數分解成為獨立變數            extract($this->tVar, EXTR_OVERWRITE);            // 直接載入PHP模板            empty($content)?include $templateFile:eval('?>'.$content);        }else{            // 視圖解析標籤            $params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix);            tag('view_parse',$params);        }        // 擷取並清空緩衝        $content = ob_get_clean();        // 內容過濾標籤        tag('view_filter',$content);        // 輸出模板檔案        return $content;    }

簡單解釋下 $this->tVar 是一個數組,當你$this->assign('a','b')的時候,就會自動添加$this->tVar['a']='b',然後通過extract()函數將數組轉化為獨立變數,即$a='b';這隻在PHP原生模板時生效,而我們常規情況下是 else{ 之後的過程,也就是

$params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix);
tag('view_parse',$params);

這兩行代碼,$params是將一些變數組成一個數組,通過tag調用view_parse處的行為(在conf/tags.php中定義),該行為類是lib/behavior/ParseTemplateBehavior.class.php;

這個行為來解析視表徵圖簽。

3.ParseTemplateBehavior 模板解析核心行為

行為通過run()方法開始解析,而上面的$params作為引用參數傳遞到run(&$_data)中。即$_data=&$params,下面從run()方法分析。
篇幅關係,這裡就不打代碼了。

大致流程如下:

檢測模板快取檔案是否存在並且有效,存在的話直接include它,如果不存在,則調用ThinkTemplate類下的fetch方法進行模板解析和編譯,將編譯結果寫入緩衝,並include快取檔案。

快取檔案就是將模板進列標籤替換後的一個PHP指令碼,include它的話就是輸出頁面了。

4. 模板的編譯

模板編譯函數位於ThinkTemplate.class.php中,方法是:protected function compiler($tmplContent){} 只有一個參數,就是讀模數板檔案的內容字串。

protected function compiler($tmplContent) {        //模板解析        $tmplContent =  $this->parse($tmplContent);        // 還原被替換的Literal標籤        $tmplContent =  preg_replace('/<!--###literal(\d+)###-->/eis',"\$this->restoreLiteral('\\1')",$tmplContent);        // 添加安全的程式碼        $tmplContent =  '<?php if (!defined(\'THINK_PATH\')) exit();?>'.$tmplContent;        if(C('TMPL_STRIP_SPACE')) {            /* 去除html空格與換行 */            $find           = array('~>\s+<~','~>(\s+\n|\r)~');            $replace        = array('><','>');            $tmplContent    = preg_replace($find, $replace, $tmplContent);        }        // 最佳化產生的php代碼        $tmplContent = str_replace('?><?php','',$tmplContent);        return strip_whitespace($tmplContent);    }

調用parse方法解析模板:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.