[TOC]
* * * * *
## 1 視圖基類文件分析(thinkphp/library/think/View.php)
>[info] 成員變量
* * * * *
~~~
視圖實例,模板引擎實例,模板主題,模板變量,模板變量。
protected static $instance = null;
protected $engine = null;
protected $theme = '';
protected $data = [];
視圖配置參數
protected $config = [
'theme_on' => false,
'auto_detect_theme' => false,
'var_theme' => 't',
'default_theme' => 'default',
'view_path' => '',
'view_suffix' => '.html',
'view_depr' => DS,
'view_layer' => VIEW_LAYER,
'parse_str' => [],
'engine_type' => 'think',
'namespace' => '\\think\\view\\driver\\',
];
~~~
>[info] 成員方法
* * * * *
> 構造函數
`public function __construct(array $config = [])`
> instance() 實例化視圖對象
`public static function instance(array $config = [])`
> assign() 模板變量賦值
`public function assign($name, $value = '')`
> config() 設置視圖參數
`public function config($config = '', $value = null)`
> engine() 設置當前模板引擎
`public function engine($engine, array $config = [])`
> them() 設置當前輸出模板主題
`public function theme($theme)`
> fetch() 解析模板內容并輸出
`public function fetch($template = '', $vars = [], $cache = [], $renderContent = false)`
> show() 解析內容并輸出
`public function show($content, $vars = [])`
> __set() __get() __isset() 操作模板變量數據
`public function __set($name, $value)`
`public function __get($name)`
`public function __isset($name)`
> getThemePath() 獲取當前模板路徑
`protected function getThemePath($module = '')`
> parseTemplate() 自動定位模板文件
`private function parseTemplate($template)`
> getTemplateTheme() 獲取當前模板主題
`private function getTemplateTheme($module)`
## 2 視圖操作文件
>[info] 視圖渲染有關文件及其邏輯關系
thinkphp/library/think/View.php 視圖對象類
thinkphp/library/think/Template.php 視圖渲染引擎抽象類
thinkphp/library/think/view/driver/Think.php Think類視圖渲染引擎
thinkphp/library/think/template/ 模板標簽目錄
thinkphp/library/think/template/TagLib.php 模板標簽基類TagLib
thinkphp/library/think/template/tablig/Cx.php 模板標簽擴展類Cx
thinkphp/library/think/tempalte/driver 模板文件緩存驅動目錄
thinkphp/library/think/tempalte/driver/File.php 普通模式下模板文件緩存實現
thinkphp/library/think/tempalte/driver/Sae.php Sae模式上下模板文件緩存實現
>[info] 視圖配置
與數據庫配置相同默認使用全局配置文件convention.php。
也可以在應用中自定義模板配置。
thinkphp/convention.php 視圖全局默認配置
~~~
'dispatch_success_tmpl' => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
'dispatch_error_tmpl' => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
'template_engine' => 'Think',
'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl',
'exception_ignore_type' => 0,
'error_message' => '頁面錯誤!請稍后再試~',
'error_page' => '',
'show_error_msg' => false,
~~~
其中template_engine配置是渲染引擎,默認使用Think渲染引擎。
## 2 文件分析
>[info] 1 視圖實例在控制器thinkphp/library/think/Controller.php
的構造函數__contruct()中創建
`$this->view = \think\View::instance(Config::get());`
調用View::instance()創建視圖對象
* * * * *
>[info] 2 View.php 視圖操作對象
~~~
public static function instance(array $config = [])
{
if (is_null(self::$instance)) {
self::$instance = new self($config);
}
return self::$instance;
}
~~~
這里使用了設計模式的單例模式創建全局唯一視圖對象,
關于單例模式見基礎原理的[php設計模式](http://www.hmoore.net/zmwtp/tp5/120198)。
其中只包含了think框架涉及的設計模式。
更多設計模式原理在線搜索。
new self($config) 調用View的構造方法__construct()創建View實例
~~~
public function __construct(array $config = [])
{
$this->config($config);
$this->engine($this->config['engine_type']);
}
~~~
首先調用$this->config()獲取配置參數
~~~
public function config($config = '', $value = null)
{
if (is_array($config)) {
foreach ($this->config as $key => $val) {
if (isset($config[$key])) {
$this->config[$key] = $config[$key];
}
}
} elseif (is_null($value)) {
return $this->config[$config];
} else {
$this->config[$config] = $value;
}
return $this;
}
~~~
然后調用$this->engine()創建引擎實例
~~~
public function engine($engine, array $config = [])
{
if ('php' == $engine) {
$this->engine = 'php';
} else {
$class = $this->config['namespace'] . ucwords($engine);
$this->engine = new $class($config);
}
return $this;
}
~~~
根據配置信息可知,這里創建Think引擎,
即thinkphp/library/think/view/dirver/Think.php的類型的實例。
$this->engine = new $class($config)
即$this->engine = new Think($config)。
* * * * *
>[info] 3 Think.php 模板解析引擎
~~~
public function __construct($config = [])
{
$this->template = new Template($config);
}
~~~
創建Template對象。thinkphp/library/think/Template.php
~~~
public function __construct(array $config = [])
{
$this->config['cache_path'] = RUNTIME_PATH . 'temp' . DS;
$this->config = array_merge($this->config, empty($config) ? Config::get() : $config);
$this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']);
$this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']);
$this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']);
$this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']);
$type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
$class = $this->config['namespace'] . ucwords($type);
$this->storage = new $class();
}
~~~
這里的構造函數主要用來
設置視圖渲染時的存儲目錄,
模板標簽庫,模板標簽的開始與結束標識符等。
## 3 總結
以上是think5涉及的視圖渲染相關文件的邏輯關系
從配置文件convention.php到Controller.php的構造函數__controller()
調用View::instance()準備創建視圖對象
接著View::instance()調用構造函數View->__construct()創建對應渲染引擎
think/Template/Think。即thinkphp/library/think/view/driver/Think.php
在Think.php中初始化渲染引擎,thinkphp/library/think/Template完成視圖對象的創建
控制器基于視圖對象View可以實現框架標簽庫模板文件的編譯,模板編譯結果的緩存,模板編譯文件的輸出
在函數助手文件/thinkphp/helper.php中封裝的V()方法可以簡化模板的渲染
視圖渲染操作見 使用使用范例的 [視圖渲染控制](http://www.hmoore.net/zmwtp/tp5/120197)
- 更新記錄
- 概述
- 文件索引
- 函數索引
- 章節格式
- 框架流程
- 前:章節說明
- 主:(index.php)入口
- 主:(start.php)框架引導
- 主:(App.php)應用啟動
- 主:(App.php)應用調度
- C:(Controller.php)應用控制器
- M:(Model.php)數據模型
- V:(View.php)視圖對象
- 附:(App.php)應用啟動
- 附:(base.php)全局變量
- 附:(common.php)模式配置
- 附:(convention.php)全局配置
- 附:(Loader.php)自動加載器
- 附:(Build.php)自動生成
- 附:(Hook.php)監聽回調
- 附:(Route.php)全局路由
- 附:(Response.php)數據輸出
- 附:(Log.php)日志記錄
- 附:(Exception.php)異常處理
- 框架工具
- 另:(helper.php)輔助函數
- 另:(Cache.php)數據緩存
- 另:(Cookie.php)cookie操作
- 另:(Console.php)控制臺
- 另:(Debug.php)開發調試
- 另:(Error.php)錯誤處理
- 另:(Url.php)Url操作文件
- 另:(Loader.php)加載器實例化
- 另:(Input.php)數據輸入
- 另:(Lang.php)語言包管理
- 另:(ORM.php)ORM基類
- 另:(Process.php)進程管理
- 另:(Session.php)session操作
- 另:(Template.php)模板解析
- 框架驅動
- D:(\config)配置解析
- D:(\controller)控制器擴展
- D:(\model)模型擴展
- D:(\db)數據庫驅動
- D:(\view)模板解析
- D:(\template)模板標簽庫
- D:(\session)session驅動
- D:(\cache)緩存驅動
- D:(\console)控制臺
- D:(\process)進程擴展
- T:(\traits)Trait目錄
- D:(\exception)異常實現
- D:(\log)日志驅動
- 使用范例
- 服務器與框架的安裝
- 控制器操作
- 數據模型操作
- 視圖渲染控制
- MVC開發初探
- 模塊開發
- 入口文件定義全局變量
- 運行模式開發
- 框架配置
- 自動生成應用
- 事件與插件注冊
- 路由規則注冊
- 輸出控制
- 多種應用組織
- 綜合應用
- tp框架整合后臺auto架構快速開發
- 基礎原理
- php默認全局變量
- php的魔術方法
- php命名空間
- php的自動加載
- php的composer
- php的反射
- php的trait機制
- php設計模式
- php的系統時區
- php的異常錯誤
- php的輸出控制
- php的正則表達式
- php的閉包函數
- php的會話控制
- php的接口
- php的PDO
- php的字符串操作
- php的curl
- 框架心得
- 心:整體結構
- 心:配置詳解
- 心:加載器詳解
- 心:輸入輸出詳解
- 心:url路由詳解
- 心:模板詳解
- 心:模型詳解
- 心:日志詳解
- 心:緩存詳解
- 心:控制臺詳解
- 框架更新
- 4.20(驗證類,助手函數)
- 4.27(新模型Model功能)
- 5.4(新數據庫驅動)
- 7.28(自動加載)