您好,欢迎访问一九零五行业门户网

yii2源码学习笔记(十八),_PHP教程

yii2源码学习笔记(十八),view继承了component,用于渲染视图文件:yii2\base\view.php
1 ['class' => 'yii\smarty\viewrenderer'], 69 * 'twig' => ['class' => 'yii\twig\viewrenderer'], 70 * ] 71 * ~~~ 72 * 73 * if no renderer is available for the given view file, the view file will be treated as a normal php 74 * and rendered via [[renderphpfile()]]. 75 */ 76 public $renderers; 77 /** 78 * @var string the default view file extension. this will be appended to view file names if they don't have file extensions. 79 * 默认视图文件扩展名,文件没有扩展名的情况下自动加载 80 */ 81 public $defaultextension = 'php'; 82 /** 83 * @var theme|array|string the theme object or the configuration for creating the theme object. 84 * if not set, it means theming is not enabled.主题对象或创建主题对象的配置 未设置则不启用 85 */ 86 public $theme; 87 /** 88 * @var array a list of named output blocks. the keys are the block names and the values 89 * are the corresponding block content. you can call [[beginblock()]] and [[endblock()]] 90 * to capture small fragments of a view. they can be later accessed somewhere else 91 * through this property. 92 * 一个输出块列表。键是块名称值为内容。可以调用 [beginblock()]和[endblock()]捕获视图的小片段 93 * 可以在其他地方通过这个属性访问。 94 */ 95 public $blocks; 96 /** 97 * @var array a list of currently active fragment cache widgets. this property 98 * is used internally to implement the content caching feature. do not modify it directly. 99 * 当前操作片段的缓存部件列表。用于内部实现内容缓存功能。不要直接修改100 * @internal101 */102 public $cachestack = [];103 /**104 * @var array a list of placeholders for embedding dynamic contents. this property105 * is used internally to implement the content caching feature. do not modify it directly.106 * 嵌入动态内容占位符列表。 用于内部实现内容缓存功能。不要直接修改107 * @internal108 */109 public $dynamicplaceholders = [];110 111 /**112 * @var array the view files currently being rendered. there may be multiple view files being113 * rendered at a moment because one view may be rendered within another.114 * 正在渲染的视图文件。可能有多个视图文件被渲染,因为一个视图可以在另一个视图中呈现115 */116 private $_viewfiles = [];117 118 119 /**120 * initializes the view component.初始化视图组件121 */122 public function init()123 {124 parent::init(); //调用父类的方法125 if (is_array($this->theme)) {126 if (!isset($this->theme['class'])) {127 $this->theme['class'] = 'yii\base\theme';//是数组,没有设置类名,则类名'yii\base\theme'128 }129 $this->theme = yii::createobject($this->theme);//设置了类名,调用配置创建对象130 } elseif (is_string($this->theme)) {//以字符串参数的形式创建对象131 $this->theme = yii::createobject($this->theme);132 }133 }134 135 /**136 * renders a view.137 * 渲染一个视图138 * the view to be rendered can be specified in one of the following formats:139 *140 * - path alias (e.g. @app/views/site/index);141 * 路径别名142 * - absolute path within application (e.g. //site/index): the view name starts with double slashes.143 * the actual view file will be looked for under the [[application::viewpath|view path]] of the application.144 * 绝对路径,会在[application::viewpath|view path]下查找文件145 * - absolute path within current module (e.g. /site/index): the view name starts with a single slash.146 * the actual view file will be looked for under the [[module::viewpath|view path]] of the [[controller::module|current module]].147 * 模块下的绝对路径,会在[module::viewpath|view path]下查找文件148 * - relative view (e.g. index): the view name does not start with `@` or `/`. the corresponding view file will be149 * looked for under the [[viewcontextinterface::getviewpath()|view path]] of the view `$context`.150 * 相对路径,会在[viewcontextinterface::getviewpath()|view path]下查找文件 151 * if `$context` is not given, it will be looked for under the directory containing the view currently152 * being rendered (i.e., this happens when rendering a view within another view).153 *154 * @param string $view the view name. 视图名称155 * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.156 * 视图中应用参数157 * @param object $context the context to be assigned to the view and can later be accessed via [[context]]158 * in the view. if the context implements [[viewcontextinterface]], it may also be used to locate159 * the view file corresponding to a relative view name. 对应情景160 * @return string the rendering result161 * @throws invalidparamexception if the view cannot be resolved or the view file does not exist.162 * @see renderfile()163 */164 public function render($view, $params = [], $context = null)165 {166 $viewfile = $this->findviewfile($view, $context);//查找视图文件路径167 return $this->renderfile($viewfile, $params, $context);//渲染视图文件168 }169 170 /**171 * finds the view file based on the given view name.通过视图文件名查找视图文件172 * @param string $view the view name or the path alias of the view file. please refer to [[render()]]173 * on how to specify this parameter. 视图名称或路径视图文件的别名174 * @param object $context the context to be assigned to the view and can later be accessed via [[context]]175 * in the view. if the context implements [[viewcontextinterface]], it may also be used to locate176 * the view file corresponding to a relative view name. 对应情景177 * @return string the view file path. note that the file may not exist. 文件路径178 * @throws invalidcallexception if a relative view name is given while there is no active context to179 * determine the corresponding view file.180 */181 protected function findviewfile($view, $context = null)182 {183 if (strncmp($view, '@', 1) === 0) {184 // e.g. @app/views/main 判断是否是别名路径,是则获取真实路径185 $file = yii::getalias($view);186 } elseif (strncmp($view, '//', 2) === 0) {187 // e.g. //layouts/main 以//开始,查找文件路径,拼接视图文件路径188 $file = yii::$app->getviewpath() . directory_separator . ltrim($view, '/');189 } elseif (strncmp($view, '/', 1) === 0) {190 // e.g. /site/index191 if (yii::$app->controller !== null) { 192 //以/开始,且控制器存在,查找控制器对应的文件目录,拼接路径193 $file = yii::$app->controller->module->getviewpath() . directory_separator . ltrim($view, '/');194 } else {195 throw new invalidcallexception(unable to locate view file for view '$view': no active controller.);196 }197 } elseif ($context instanceof viewcontextinterface) {198 //对应情景存在 查找文件路径,拼接视图文件路径199 $file = $context->getviewpath() . directory_separator . $view;200 } elseif (($currentviewfile = $this->getviewfile()) !== false) {201 //当前渲染文件存在,拼接路径202 $file = dirname($currentviewfile) . directory_separator . $view;203 } else {204 throw new invalidcallexception(unable to resolve view file for view '$view': no active view context.);205 }206 207 if (pathinfo($file, pathinfo_extension) !== '') {208 return $file;//视图文件的扩展名不为空,返回扩展名209 }210 $path = $file . '.' . $this->defaultextension; //给视图文件添加扩展名211 if ($this->defaultextension !== 'php' && !is_file($path)) {212 $path = $file . '.php';213 }214 215 return $path;//返回路径216 }217 218 /**219 * renders a view file.220 * 渲染一个视图文件。221 * if [[theme]] is enabled (not null), it will try to render the themed version of the view file as long222 * as it is available.223 * 如果[theme]可用,将渲染视图文件的主题版本直到[theme]不可用224 * the method will call [[filehelper::localize()]] to localize the view file.225 * 调用[filehelper::localize()]方法本地化视图文件226 * if [[renderers|renderer]] is enabled (not null), the method will use it to render the view file.227 * otherwise, it will simply include the view file as a normal php file, capture its output and228 * return it as a string.229 * 如果[[renderers|renderer]]启用,该方法将用它来渲染视图文件。否则,将视图文件作为一个正常的php文件包含进来,获取其输出并返回一个字符串。230 * @param string $viewfile the view file. this can be either an absolute file path or an alias of it.231 * 视图文件。可以是绝对路径或它的别名。232 * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.233 * 视图文件执行的参数234 * @param object $context the context that the view should use for rendering the view. if null,235 * existing [[context]] will be used.236 * 用于渲染视图的上下文237 * @return string the rendering result 238 * @throws invalidparamexception if the view file does not exist239 */240 public function renderfile($viewfile, $params = [], $context = null)241 {242 $viewfile = yii::getalias($viewfile);//处理输入的视图文件名243 244 if ($this->theme !== null) {245 $viewfile = $this->theme->applyto($viewfile);//如果theme非空,应用到视图文件246 }247 if (is_file($viewfile)) {248 $viewfile = filehelper::localize($viewfile);//本地化视图文件249 } else {250 throw new invalidparamexception(the view file does not exist: $viewfile);251 }252 253 $oldcontext = $this->context;254 if ($context !== null) {255 $this->context = $context;256 }257 $output = '';258 $this->_viewfiles[] = $viewfile;//记录当前渲染文件259 260 if ($this->beforerender($viewfile, $params)) {//如果前置事件执行成功261 yii::trace(rendering view file: $viewfile, __method__);//记录trace信息262 $ext = pathinfo($viewfile, pathinfo_extension);//视图文件扩展名263 if (isset($this->renderers[$ext])) {//视图文件的扩展名是否支持264 if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {265 $this->renderers[$ext] = yii::createobject($this->renderers[$ext]);266 }267 /* @var $renderer viewrenderer */268 $renderer = $this->renderers[$ext];//赋值view渲染器对象269 $output = $renderer->render($this, $viewfile, $params);//渲染视图文件270 } else {//视图文件不是支持的类型,以普通php文件处理271 $output = $this->renderphpfile($viewfile, $params);272 }273 $this->afterrender($viewfile, $params, $output);274 }275 276 array_pop($this->_viewfiles);277 $this->context = $oldcontext;278 279 return $output;280 }
http://www.bkjia.com/phpjc/1136181.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1136181.htmltecharticleyii2源码学习笔记(十八), view继承了component,用于渲染视图文件:yii2\base\view.php 1 ? php 2 /* * 3 * @link http://www.yiiframework.com/ 4 * @copyright co...
其它类似信息

推荐信息