表面上看,TP輸出一個頁面很簡單:$this->display();
實際上是怎么回事呢?$this->display(); 這個display()方法是定義在ThinkPHP/Library/Think/Controller.class.php這個文件中的
protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {$this->view->display($templateFile,$charset,$contentType,$content,$prefix);
}
而這個display方法其實是屬于$this->view這個對象的。$this->view 是什么呢?還是ThinkPHP/Library/Think/Controller.class.php這個文件
public function __construct() {Hook::listen('action_begin',$this->config);//實例化視圖類$this->view = Think::instance('Think\View');//控制器初始化if(method_exists($this,'_initialize'))$this->_initialize();}
可以看到$this->view其實是Think\View類的一個實例,Think\View 就是ThinkPHP/Library/Think/View.class.php啦
那我們就去看看View.class.php中的display方法是長什么樣的
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');}
忽略G方法、鉤子, 就是簡單的兩部分內容:1、解析并獲取模板內容;2、輸出模板內容;
具體怎么回事呢?
第一點是有兩部分的,首先獲取模板文件的位置($this->parseTemplate) ,然后把里面的PHP標簽替換成具體的內容
第二點呢,太簡單了,直接echo $content,就輸出內容了
到此,$this->display()背后的過程就清清楚楚了。
?