[TOC]
* * * * *
# 1 視圖對象
>>視圖(View),作為(MVC)的一員,代表對輸出數據的Web界面組織操作對象。
> 視圖可以用來存儲模板變量
> 最終調用模板引擎 將(模板變量) 與 (模板文件)解析為輸出的Web界面
### View::instance()
>>單例模式,創建全局唯一的視圖對象
~~~
public static function instance($engine = [], $replace = [])
{
if (is_null(self::$instance)) {
self::$instance = new self($engine, $replace);
}
return self::$instance;
}
~~~
### $view->__construct()
>>視圖構造函數,創建視圖對象
~~~
public function __construct($engine = [], $replace = [])
{
// 初始化模板引擎
$this->engine((array) $engine);
$this->replace = $replace;
}
~~~
### $view->engine()
>>模板引擎創建,根據配置創建相應的模板引擎(默認為Think)
~~~
public function engine($options = [])
{
if (is_string($options)) {
$type = $options;
$options = [];
} else {
$type = !empty($options['type']) ? $options['type'] : 'Think';
}
$class = false !== strpos($type, '\\') ? $type : '\\think\\view\\driver\\' . ucfirst($type);
if (isset($options['type'])) {
unset($options['type']);
}
$this->engine = new $class($options);
return $this;
}
~~~
>>由上可知,默認的模板引擎(Think)實現為view\driver\Think.php文件。模板引擎具體的信息見 模板引擎 章節
### $view->config()
>>模板引擎配置,配置模板引擎參數
~~~
public function config($name, $value = null)
{
$this->engine->config($name, $value);
return $this;
}
~~~
# 2 視圖操作
## 2-1 模板變量
### $view->assign()
>>存儲數據到模板變量中。
~~~
public function assign($name, $value = '')
{
if (is_array($name)) {
$this->data = array_merge($this->data, $name);
} else {
$this->data[$name] = $value;
}
return $this;
}
~~~
### $view->__get()
>> 模板變量獲取快捷操作
~~~
public function __get($name)
{
return $this->data[$name];
}
~~~
### $view->__set()
>> 模板變量賦值快捷操作
~~~
public function __set($name, $value)
{
$this->data[$name] = $value;
}
~~~
### $view->__isset()
>> 模板變量檢測快捷操作
~~~
public function __isset($name)
{
return isset($this->data[$name]);
}
~~~
## 2-2 模板文件
>>模板文件,作為界面組織的文件。模板引擎將模板變量填充到模板文件中生成輸出的Web頁面
### $view->replace()
>> 模板字符串替換內容配置
~~~
public function replace($content, $replace = '')
{
if (is_array($content)) {
$this->replace = array_merge($this->replace, $content);
} else {
$this->replace[$content] = $replace;
}
return $this;
}
~~~
### $view->fetch()
>> 獲取模板文件解析結果
~~~
public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false)
{
// 模板變量
$vars = array_merge($this->data, $vars);
// 頁面緩存
ob_start();
ob_implicit_flush(0);
// 渲染輸出
$method = $renderContent ? 'display' : 'fetch';
$this->engine->$method($template, $vars, $config);
// 獲取并清空緩存
$content = ob_get_clean();
// 內容過濾標簽
Hook::listen('view_filter', $content);
// 允許用戶自定義模板的字符串替換
$replace = array_merge($this->replace, $replace);
if (!empty($replace)) {
$content = strtr($content, $replace);
}
return $content;
}
~~~
### $view->display()
>> 輸出模板文件解析結果
~~~
public function display($content, $vars = [], $replace = [], $config = [])
{
return $this->fetch($content, $vars, $replace, $config, true);
}
~~~
- 框架簡介
- 簡介
- 框架目錄
- 根目錄
- 應用目錄
- 核心目錄
- 擴展目錄
- 其他目錄
- 框架流程
- 啟動流程
- 請求流程
- 響應流程
- 框架結構
- 應用組織
- 網絡請求
- 路由組織
- 數據驗證
- 數據模型(M)
- 數據庫連接(Connection)
- 數據庫(Db)
- 查詢構造(Builder)
- 數據庫查詢(Query)
- 模型(Model)
- 模板視圖(V)
- 視圖(View)
- 模板引擎(Think)
- 模板標簽庫(TagLib)
- 控制器(C)
- 網絡響應
- 配置與緩存
- 配置操作
- 緩存操作
- cookie與session
- Cookie操作
- Session操作
- 自動加載
- 鉤子注冊
- 文件上傳
- 分頁控制
- 控制臺
- 自動構建
- 日志異常調試
- 異常處理
- 代碼調試
- 日志記錄
- 框架使用
- 1 環境搭建(Server)
- 2 網絡請求(Request)
- 3 請求路由(Route)
- 4 響應輸出(Response)
- 5 業務處理(Controller)
- 6 數據存取(Model)
- 7 Web界面(View)