<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看