Yii源码阅读笔记(十九)

View中渲染view视图文件的前置和后置方法,以及渲染动态内容的方法:

  1   /**
  2      * @return string|boolean the view file currently being rendered. False if no view file is being rendered.
  3      */
  4     public function getViewFile()
  5     {
  6         return end($this->_viewFiles);//返回[_viewFiles]中的最后一个view文件,即默认被渲染的文件
  7     }
  8
  9     /**
 10      * This method is invoked right before [[renderFile()]] renders a view file.
 11      * Render的前置事件,在执行[renderFile()]方法时被调用,默认触发[[EVENT_BEFORE_RENDER]]事件
 12      * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
 13      * If you override this method, make sure you call the parent implementation first.
 14      * 如果要重写该方法,要确保首先调用父类的同名方法
 15      * @param string $viewFile the view file to be rendered.
 16      * @param array $params the parameter array passed to the [[render()]] method.
 17      * @return boolean whether to continue rendering the view file.
 18      */
 19     public function beforeRender($viewFile, $params)
 20     {
 21         //实例化ViewEvent
 22         $event = new ViewEvent([
 23             ‘viewFile‘ => $viewFile,
 24             ‘params‘ => $params,
 25         ]);
 26         //触发[EVENT_BEFORE_RENDER]事件
 27         $this->trigger(self::EVENT_BEFORE_RENDER, $event);
 28
 29         return $event->isValid;//返回值可以判断是否继续渲染文件
 30     }
 31
 32     /**
 33      * This method is invoked right after [[renderFile()]] renders a view file.
 34      * Render的后置事件,在执行[renderFile()]方法后被调用,默认触发[[EVENT_AFTER_RENDER]]事件
 35      * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
 36      * If you override this method, make sure you call the parent implementation first.
 37      * 如果要重写该方法,要确保首先调用父类的同名方法
 38      * @param string $viewFile the view file being rendered.
 39      * @param array $params the parameter array passed to the [[render()]] method.
 40      * @param string $output the rendering result of the view file. Updates to this parameter
 41      * will be passed back and returned by [[renderFile()]].
 42      */
 43     public function afterRender($viewFile, $params, &$output)
 44     {
 45         if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {//判断[EVENT_AFTER_RENDER]事件是否有处理函数
 46            //实例化ViewEvent
 47             $event = new ViewEvent([
 48                 ‘viewFile‘ => $viewFile,
 49                 ‘params‘ => $params,
 50                 ‘output‘ => $output,
 51             ]);
 52              //触发[EVENT_AFTER_RENDER]事件
 53             $this->trigger(self::EVENT_AFTER_RENDER, $event);
 54             $output = $event->output;//执行后置事件后的输出结果
 55         }
 56     }
 57
 58     /**
 59      * Renders a view file as a PHP script.
 60      * 将一个view文件当作PHP脚本渲染
 61      * This method treats the view file as a PHP script and includes the file.
 62      * It extracts the given parameters and makes them available in the view file.
 63      * The method captures the output of the included view file and returns it as a string.
 64      * 将传入的参数转换为变量,包含并执行view文件,返回执行结果
 65      * This method should mainly be called by view renderer or [[renderFile()]].
 66      *
 67      * @param string $_file_ the view file.
 68      * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
 69      * @return string the rendering result
 70      */
 71     public function renderPhpFile($_file_, $_params_ = [])
 72     {
 73         //ob_start() — 打开输出控制缓冲
 74         ob_start();
 75         // ob_implicit_flush ()  — 默认为关闭缓冲区,打开绝对输出后,每个脚本输出都直接发送到浏览器,不再需要调用 flush()
 76         ob_implicit_flush(false);
 77         extract($_params_, EXTR_OVERWRITE);//extract() - 用于将一个数组转换为变量使用,键名为变量名,键值为对应的变量值
 78         require($_file_);
 79         //ob_get_clean() — 得到当前缓冲区的内容并删除当前输出缓
 80         return ob_get_clean();
 81     }
 82
 83     /**
 84      * Renders dynamic content returned by the given PHP statements.
 85      * 渲染动态内容
 86      * This method is mainly used together with content caching (fragment caching and page caching)
 87      * 该方法主要用来聚合缓存的内容(片段缓存和页面缓存)
 88      * when some portions of the content (called *dynamic content*) should not be cached.
 89      * The dynamic content must be returned by some PHP statements.
 90      * 用来渲染某些被PHP语句返回的动态内容
 91      * @param string $statements the PHP statements for generating the dynamic content.
 92      * @return string the placeholder of the dynamic content, or the dynamic content if there is no
 93      * active content cache currently.
 94      */
 95     public function renderDynamic($statements)
 96     {
 97         if (!empty($this->cacheStack)) {//动态内容的栈列表不为空
 98             $n = count($this->dynamicPlaceholders);//计算动态内容条数
 99             $placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";//生成占位符--动态内容前缀--起标记作用
100             $this->addDynamicPlaceholder($placeholder, $statements);//添加动态内容占位符
101
102             return $placeholder;
103         } else {
104             return $this->evaluateDynamicContent($statements);//动态内容的栈列表为空,值行传入的PHP语句,返回执行结果
105         }
106     }
107
108     /**
109      * Adds a placeholder for dynamic content.
110      * 给dynamic content添加一个占位符
111      * This method is internally used.
112      * 该方法是内部使用的
113      * @param string $placeholder the placeholder name
114      * @param string $statements the PHP statements for generating the dynamic content
115      */
116     public function addDynamicPlaceholder($placeholder, $statements)
117     {
118         foreach ($this->cacheStack as $cache) {
119             $cache->dynamicPlaceholders[$placeholder] = $statements;//给widget中的[FragmentCache]添加占位符
120         }
121         $this->dynamicPlaceholders[$placeholder] = $statements;//给当前视图添加动态内容占位符
122     }
123
124     /**
125      * Evaluates the given PHP statements.
126      * 求给定的PHP语句的值
127      * This method is mainly used internally to implement dynamic content feature.
128      * 该方法是内部使用实现动态内容功能
129      * @param string $statements the PHP statements to be evaluated.
130      * @return mixed the return value of the PHP statements.
131      */
132     public function evaluateDynamicContent($statements)
133     {
134         return eval($statements);//eval() 函数用于执行文本方式输入的php语句
135     }

GitHub地址: https://github.com/mogambos/yii-2.0.7/blob/master/vendor/yiisoft/yii2/base/View.php

时间: 2024-10-13 02:39:36

Yii源码阅读笔记(十九)的相关文章

Yii源码阅读笔记(九)

Behvaior类,Behavior类是所有事件类的基类: 1 namespace yii\base; 2 3 /** 4 * Behavior is the base class for all behavior classes. 5 * 所有行为的基类 6 * A behavior can be used to enhance the functionality of an existing component without modifying its code. 7 * In parti

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

Yii源码阅读笔记(一)

今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于web目录的index.php,用于启动web应用和配置一些路径参数. index.php—— 1 // comment out the following two lines when deployed to production 2 defined('YII_DEBUG') or define('Y

Yii源码阅读笔记(二十九)

动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel is a model class primarily used to support ad hoc data validation. * DynamicModel是一种主要用于支持ad hoc数据验证模型类 * * The typical usage of DynamicModel is as fo

Yii源码阅读笔记(二十六)

Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: 1 /** 2 * Handles the specified request. 3 * 处理指定的请求--抽象方法 4 * This method should return an instance of [[Response]] or its child class 5 * which represents the handling result of the reques

Yii源码阅读笔记(三十五)

Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. 1 namespace yii\di; 2 3 use ReflectionClass; 4 use Yii; 5 use yii\base\Component; 6 use yii\base\InvalidConfigException; 7 use yii\helpers\ArrayHelper; 8 9 /** 10 * Container implements

Yii源码阅读笔记(三十二)

web/Application类的注释,继承base/Application类,针对web应用的一些处理: 1 namespace yii\web; 2 3 use Yii; 4 use yii\base\InvalidRouteException; 5 6 /** 7 * Application is the base class for all web application classes. 8 * Application 是所有web应用的基类 9 * 10 * @property st

Yii源码阅读笔记(二十二)

Module类,属性的注释和构造函数的注释: 1 <?php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright Copyright (c) 2008 Yii Software LLC 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 namespace yii\base; 9 10 use Yii; 11 use yii\di\ServiceLocator;

Yii源码阅读笔记(十二)

Action类,控制器中方法的基类: 1 namespace yii\base; 2 3 use Yii; 4 5 /** 6 * Action is the base class for all controller action classes. 7 * Action是所有控制器方法的基类 8 * Action provides a way to reuse action method code. An action method in an Action 9 * class can be