企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[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.kancloud.cn/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.kancloud.cn/zmwtp/tp5/120197)