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

   /**
      * @return string|boolean the view file currently being rendered. False if no view file is being rendered.
      */
     public function getViewFile()
     {
         return end($this->_viewFiles);//返回[_viewFiles]中的最后一个view文件,即默认被渲染的文件
     }

     /**
      * This method is invoked right before [[renderFile()]] renders a view file.
      * Render的前置事件,在执行[renderFile()]方法时被调用,默认触发[[EVENT_BEFORE_RENDER]]事件
      * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
      * If you override this method, make sure you call the parent implementation first.
      * 如果要重写该方法,要确保首先调用父类的同名方法
      * @param string $viewFile the view file to be rendered.
      * @param array $params the parameter array passed to the [[render()]] method.
      * @return boolean whether to continue rendering the view file.
      */
     public function beforeRender($viewFile, $params)
     {
         //实例化ViewEvent
         $event = new ViewEvent([
             'viewFile' => $viewFile,
             'params' => $params,
         ]);
         //触发[EVENT_BEFORE_RENDER]事件
         $this->trigger(self::EVENT_BEFORE_RENDER, $event);

         return $event->isValid;//返回值可以判断是否继续渲染文件
     }

     /**
      * This method is invoked right after [[renderFile()]] renders a view file.
      * Render的后置事件,在执行[renderFile()]方法后被调用,默认触发[[EVENT_AFTER_RENDER]]事件
      * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
      * If you override this method, make sure you call the parent implementation first.
      * 如果要重写该方法,要确保首先调用父类的同名方法
      * @param string $viewFile the view file being rendered.
      * @param array $params the parameter array passed to the [[render()]] method.
      * @param string $output the rendering result of the view file. Updates to this parameter
      * will be passed back and returned by [[renderFile()]].
      */
     public function afterRender($viewFile, $params, &$output)
     {
         if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {//判断[EVENT_AFTER_RENDER]事件是否有处理函数
            //实例化ViewEvent
             $event = new ViewEvent([
                 'viewFile' => $viewFile,
                 'params' => $params,
                 'output' => $output,
             ]);
              //触发[EVENT_AFTER_RENDER]事件
             $this->trigger(self::EVENT_AFTER_RENDER, $event);
             $output = $event->output;//执行后置事件后的输出结果
         }
     }

     /**
      * Renders a view file as a PHP script.
      * 将一个view文件当作PHP脚本渲染
      * This method treats the view file as a PHP script and includes the file.
      * It extracts the given parameters and makes them available in the view file.
      * The method captures the output of the included view file and returns it as a string.
      * 将传入的参数转换为变量,包含并执行view文件,返回执行结果
      * This method should mainly be called by view renderer or [[renderFile()]].
      *
      * @param string $_file_ the view file.
      * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
      * @return string the rendering result
      */
     public function renderPhpFile($_file_, $_params_ = [])
     {
         //ob_start() — 打开输出控制缓冲
         ob_start();
         // ob_implicit_flush ()  — 默认为关闭缓冲区,打开绝对输出后,每个脚本输出都直接发送到浏览器,不再需要调用 flush()
         ob_implicit_flush(false);
         extract($_params_, EXTR_OVERWRITE);//extract() - 用于将一个数组转换为变量使用,键名为变量名,键值为对应的变量值
         require($_file_);
         //ob_get_clean() — 得到当前缓冲区的内容并删除当前输出缓
         return ob_get_clean();
     }

     /**
      * Renders dynamic content returned by the given PHP statements.
      * 渲染动态内容
      * This method is mainly used together with content caching (fragment caching and page caching)
      * 该方法主要用来聚合缓存的内容(片段缓存和页面缓存)
      * when some portions of the content (called *dynamic content*) should not be cached.
      * The dynamic content must be returned by some PHP statements.
      * 用来渲染某些被PHP语句返回的动态内容
      * @param string $statements the PHP statements for generating the dynamic content.
      * @return string the placeholder of the dynamic content, or the dynamic content if there is no
      * active content cache currently.
      */
     public function renderDynamic($statements)
     {
         if (!empty($this->cacheStack)) {//动态内容的栈列表不为空
             $n = count($this->dynamicPlaceholders);//计算动态内容条数
             $placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";//生成占位符--动态内容前缀--起标记作用
             $this->addDynamicPlaceholder($placeholder, $statements);//添加动态内容占位符

             return $placeholder;
         } else {
             return $this->evaluateDynamicContent($statements);//动态内容的栈列表为空,值行传入的PHP语句,返回执行结果
         }
     }

     /**
      * Adds a placeholder for dynamic content.
      * 给dynamic content添加一个占位符
      * This method is internally used.
      * 该方法是内部使用的
      * @param string $placeholder the placeholder name
      * @param string $statements the PHP statements for generating the dynamic content
      */
     public function addDynamicPlaceholder($placeholder, $statements)
     {
         foreach ($this->cacheStack as $cache) {
             $cache->dynamicPlaceholders[$placeholder] = $statements;//给widget中的[FragmentCache]添加占位符
         }
         $this->dynamicPlaceholders[$placeholder] = $statements;//给当前视图添加动态内容占位符
     }

     /**
      * Evaluates the given PHP statements.
      * 求给定的PHP语句的值
      * This method is mainly used internally to implement dynamic content feature.
      * 该方法是内部使用实现动态内容功能
      * @param string $statements the PHP statements to be evaluated.
      * @return mixed the return value of the PHP statements.
      */
     public function evaluateDynamicContent($statements)
     {
         return eval($statements);//eval() 函数用于执行文本方式输入的php语句
     }

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

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

  1. Yii源码阅读笔记(九)

    Behvaior类,Behavior类是所有事件类的基类: namespace yii\base; /** * Behavior is the base class for all behavior ...

  2. Yii源码阅读笔记(一)

    今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...

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

    动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel ...

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

    Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...

  5. Yii源码阅读笔记(三十四)

    Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...

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

    web/Application类的注释,继承base/Application类,针对web应用的一些处理: namespace yii\web; use Yii; use yii\base\Inval ...

  7. Yii源码阅读笔记(三十)

    Widget类是所有小部件的基类,开始,结束和渲染小部件内容的方法的注释: namespace yii\base; use Yii; use ReflectionClass; /** * Widget ...

  8. Yii源码阅读笔记(二十八)

    Yii/web中的Controller类,实现参数绑定,启动csrf验证功能,重定向页面功能: namespace yii\web; use Yii; use yii\base\InlineActio ...

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

    Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: /** * Handles the specified request. * 处 ...

随机推荐

  1. SQL如何在数据库间复制表

    方法一: DB1  tb1 DB2  tb2 选择DB1 到表的列表那里选择tb1表 右键 所有任务 数据导出 下一步  选择你要导出的数据库DB1  下一步 选择你要导入的数据库DB2 下一步  选 ...

  2. CI框架获取post和get参数_CodeIgniter心得

    请参考:CI文档的输入类部分: $this->input->post() $this->input->get() ------------------------------- ...

  3. hrbustoj 1073:病毒(并查集,入门题)

    病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...

  4. poj 3264 RMQ 水题

    题意:找到一段数字里最大值和最小值的差 水题 #include<cstdio> #include<iostream> #include<algorithm> #in ...

  5. cocos2dx游戏开发——微信打飞机学习笔记(十)——碰撞检测的搭建

    一.七说八说        大家都发现了= =,做了那么多,发现就是摆设,完全没有打飞机的感觉,没有实现碰撞的监测.比如说呢,子弹和敌机,玩家与敌机就是需要有碰撞检测的说,然后在这篇我想会很长很长的教 ...

  6. Distinct和Group by去除重复字段记录

    重复记录 有两个意义,一是完全重复的记录,也即所有字段均重复的记录 二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略. 1.对于第一种重复,比较容易解决,使用 s ...

  7. Enter直接登录

    2.2  按Enter键调用登录按钮 [实例描述] 为了方便用户操作,在登录邮箱或论坛时,如果用户输入了用户名和密码,按Enter键时,都会自动调用登录按钮.本例学习如何实现此功能. [实现代码] & ...

  8. MongoDB安装(一)

    下载 上MongoDB官网 ,我们发现有32bit和64bit,这个就要看你系统了,不过这里有两点注意: ①:根据业界规则,偶数为“稳定版”(如:1.6.X,1.8.X),奇数为“开发版”(如:1.7 ...

  9. Spring的bean标签

    Spring框架中主要有四种标签bean.alias.import.beans,其中bean标签是其他标签的基础. 一.bean标签的属性 scope:用来配置spring bean的作用域 sing ...

  10. 递推DP URAL 1081 Binary Lexicographic Sequence

    题目传送门 题意:问第k个长度为n的01串是什么(不能有相邻的1) 分析:dp[i][0/1] 表示前i个,当前第i个放1或0的方案数,先预处理计算,dp[i][1]只能有dp[i-1][0]转移过来 ...