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);
} /**
* This method is invoked right before [[renderFile()]] renders a view file.
* The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
* 前置事件,执行[renderFile()]时被调用,默认触发[[EVENT_BEFORE_RENDER]]事件
* 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.
* 参数数组传递到[render()]方法。
* @return boolean whether to continue rendering the view file. 是否继续渲染视图文件。
*/
public function beforeRender($viewFile, $params)
{
$event = new ViewEvent([//实例化ViewEvent
'viewFile' => $viewFile,
'params' => $params,
]);
$this->trigger(self::EVENT_BEFORE_RENDER, $event);//触发[EVENT_BEFORE_RENDER]事件 return $event->isValid;//判断是否继续渲染文件
} /**
* This method is invoked right after [[renderFile()]] renders a view file.
* The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
* 后置事件,在执行[renderFile()]方法后被调用,默认触发[[EVENT_AFTER_RENDER]]事件
* 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.
* 参数数组传递到[render()]方法。
* @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]事件是否存在
$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.
* 返回一个视图文件当作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_implicit_flush(false); //关闭缓冲区
extract($_params_, EXTR_OVERWRITE);// 将一个数组转换为变量使用
require($_file_); 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.生成动态内容的PHP语句。
* @return string the placeholder of the dynamic content, or the dynamic content if there is no
* active content cache currently. 动态内容占位符 如果当前没有有效的内容缓存,调用evaluateDynamicContent输出
*/
public function renderDynamic($statements)
{
if (!empty($this->cacheStack)) {//动态内容的列表不为空
$n = count($this->dynamicPlaceholders);//统计动态内容条数
$placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";//生成占位符
$this->addDynamicPlaceholder($placeholder, $statements);//添加动态内容占位符 return $placeholder;
} else {//没有有效缓存 执行传入的PHP语句,返回执行结果
return $this->evaluateDynamicContent($statements);
}
} /**
* Adds a placeholder for dynamic content. 添加一个动态内容占位符
* This method is internally used. 内部使用
* @param string $placeholder the placeholder name 占位符名称
* @param string $statements the PHP statements for generating the dynamic content
* 生成动态内容的PHP语句
*/
public function addDynamicPlaceholder($placeholder, $statements)
{
foreach ($this->cacheStack as $cache) {
$cache->dynamicPlaceholders[$placeholder] = $statements;//添加动态内容占位符
}
$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. PHP语句进行计算
* @return mixed the return value of the PHP statements. PHP语句的值
*/
public function evaluateDynamicContent($statements)
{
return eval($statements);
} /**
* Begins recording a block.
* This method is a shortcut to beginning [[Block]]
* 数据块开始的标记,该方法是开始[Block]的快捷方式
* 数据块可以在一个地方指定视图内容在另一个地方显示,通常和布局一起使用
* @param string $id the block ID. 数据块标识
* @param boolean $renderInPlace whether to render the block content in place. 是否渲染块内容。
* Defaults to false, meaning the captured block will not be displayed.
* @return Block the Block widget instance 数据块部件实例
*/
public function beginBlock($id, $renderInPlace = false)
{
return Block::begin([
'id' => $id,//数据块唯一标识
'renderInPlace' => $renderInPlace,//是否显示标识
'view' => $this,
]);
} /**
* Ends recording a block. 数据块结束标识
*/
public function endBlock()
{
Block::end();
} /**
* Begins the rendering of content that is to be decorated by the specified view.
* This method can be used to implement nested layout. For example, a layout can be embedded
* in another layout file specified as '@app/views/layouts/base.php' like the following:
* 开始指定的view渲染内容,用来实现嵌套布局,传入的第一个参数为布局文件的路径
* ~~~
* <?php $this->beginContent('@app/views/layouts/base.php'); ?>
* ...layout content here...
* <?php $this->endContent(); ?>
* ~~~
*
* @param string $viewFile the view file that will be used to decorate the content enclosed by this widget.
* This can be specified as either the view file path or path alias.布局文件的路径或路径别名。
* @param array $params the variables (name => value) to be extracted and made available in the decorative view.
* 可以在视图中运用的参数
* @return ContentDecorator the ContentDecorator widget instance 部件实例
* @see ContentDecorator
*/
public function beginContent($viewFile, $params = [])
{
return ContentDecorator::begin([
'viewFile' => $viewFile,
'params' => $params,
'view' => $this,
]);
} /**
* Ends the rendering of content.结束渲染内容
*/
public function endContent()
{
ContentDecorator::end();
} /**
* Begins fragment caching. 开始片段缓存
* This method will display cached content if it is available.
* If not, it will start caching and would expect an [[endCache()]]
* call to end the cache and save the content into cache.
* 展示可用的缓存内容,否则将开始缓存内容直到出现[endCache()]方法
* A typical usage of fragment caching is as follows,
*
* ~~~
* if ($this->beginCache($id)) {
* // ...generate content here
* $this->endCache();
* }
* ~~~
*
* @param string $id a unique ID identifying the fragment to be cached.缓存片段的唯一标识
* @param array $properties initial property values for [[FragmentCache]]初始属性[FragmentCache]
* @return boolean whether you should generate the content for caching. 是否生成缓存的内容。
* False if the cached version is available.
*/
public function beginCache($id, $properties = [])
{
$properties['id'] = $id; //片段标识
$properties['view'] = $this; //调用初始化属性
/* @var $cache FragmentCache */
$cache = FragmentCache::begin($properties);
if ($cache->getCachedContent() !== false) {
$this->endCache();//从缓存中读取到了缓存的内容,则渲染内容并返回 false,不再进行缓存 return false;
} else {
return true;
}
} /**
* Ends fragment caching. 结束片段缓存
*/
public function endCache()
{
FragmentCache::end();
} /**
* Marks the beginning of a page.页面开始标记
*/
public function beginPage()
{
ob_start(); //打开输出缓冲
ob_implicit_flush(false);//关闭缓冲区 $this->trigger(self::EVENT_BEGIN_PAGE);
} /**
* Marks the ending of a page. 页面结束标记
*/
public function endPage()
{
$this->trigger(self::EVENT_END_PAGE);
ob_end_flush();//关闭输出缓冲区
}

yii2源码学习笔记(十九)的更多相关文章

  1. yii2源码学习笔记(十四)

    Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...

  2. yii2源码学习笔记(十二)

    继续了解controller基类. /** * Runs a request specified in terms of a route.在路径中指定的请求. * The route can be e ...

  3. yii2源码学习笔记(十)

    继续了解Application. /** * Registers the errorHandler component as a PHP error handler. * 注册errorHandler ...

  4. yii2源码学习笔记(十六)

    Module类的最后代码 /** * Registers sub-modules in the current module. * 注册子模块到当前模块 * Each sub-module shoul ...

  5. yii2源码学习笔记(十五)

    这几天有点忙今天好些了,继续上次的module来吧 /** * Returns the directory that contains the controller classes according ...

  6. yii2源码学习笔记(九)

    Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...

  7. yii2源码学习笔记(八)

    Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...

  8. 老刘 Yii2 源码学习笔记之 Action 类

    Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...

  9. yii2源码学习笔记(二十)

    Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...

随机推荐

  1. asp.net中为什么修改了配置文件后我们不需要重启IIS

    本文转载:http://blog.itpub.net/12639172/viewspace-659819/ 大家知道,asp.net中,如果我们修改了配置文件只要把它保存之后,就会立刻反应到程序中, ...

  2. ZooKeeper场景实践:(6)集群监控和Master选举

    1. 集群机器监控 这通经常使用于那种对集群中机器状态,机器在线率有较高要求的场景,可以高速对集群中机器变化作出响应.这种场景中,往往有一个监控系统,实时检測集群机器是否存活. 利用ZooKeeper ...

  3. Redis作者谈Redis应用场景

    Redis作者谈Redis应用场景 毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多 ...

  4. Static NAT with iptables on Linux

    本文的名字取的比较有意义,因为本文并不是真的要讨论如何在Linux上使用iptables实现static nat!之所以这么命名本文,是想引起别人的注意,因为中文资料,以及国内的搜索引擎,基本上没有人 ...

  5. 硬菜点播台 | MySQL阿里实践经典案例之参数调优最佳实践

    http://mp.weixin.qq.com/s?__biz=MzA4NjI4MzM4MQ%3D%3D&mid=512708319&idx=1&sn=6af5f424d7cd ...

  6. hibernate缓存技术

    1.缓存 2.Hibernate 缓存作用:为了提高查询效率. 3.第一次操作某个对象的时候,把操作的对象数据存储到缓存中,然后下一次在对同一个对象操作的时候,就不会在连接数据库. 4.Hiberna ...

  7. AWS RDS 使用笔记

    创建VPC安全组 添加VPC子网 创建RDS子网组 创建RDS参数组 创建MySQL实例 查看RDS终端节点 使用 MySQL 监视器与数据库实例上的数据库连接 安装mysql client $ su ...

  8. 第十一篇:web之Django之Form组件

    Django之Form组件   Django之Form组件 本节内容 基本使用 form中字段和插件 自定义验证规则 动态加载数据到form中 1. 基本使用 django中的Form组件有以下几个功 ...

  9. Openfire:安装指南

    本文的英文原文来自 http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/install-guide.html ...

  10. ORACLE 11g R2数据库安装硬件环境要求

    物理内存要求:最小1G,在windows7,windows8,windows8.1上最小2G. 虚拟内存(或分页空间)容量要求: Available RAM Swap Space Required B ...