view剩余代码

  1. /**
  2. * @return string|boolean the view file currently being rendered. False if no view file is being rendered.
  3. * 当前正在渲染的视图文件
  4. */
  5. public function getViewFile()
  6. {
  7. return end($this->_viewFiles);
  8. }
  9.  
  10. /**
  11. * This method is invoked right before [[renderFile()]] renders a view file.
  12. * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
  13. * 前置事件,执行[renderFile()]时被调用,默认触发[[EVENT_BEFORE_RENDER]]事件
  14. * If you override this method, make sure you call the parent implementation first.
  15. * @param string $viewFile the view file to be rendered. 要渲染的视图文件。
  16. * @param array $params the parameter array passed to the [[render()]] method.
  17. * 参数数组传递到[render()]方法。
  18. * @return boolean whether to continue rendering the view file. 是否继续渲染视图文件。
  19. */
  20. public function beforeRender($viewFile, $params)
  21. {
  22. $event = new ViewEvent([//实例化ViewEvent
  23. 'viewFile' => $viewFile,
  24. 'params' => $params,
  25. ]);
  26. $this->trigger(self::EVENT_BEFORE_RENDER, $event);//触发[EVENT_BEFORE_RENDER]事件
  27.  
  28. return $event->isValid;//判断是否继续渲染文件
  29. }
  30.  
  31. /**
  32. * This method is invoked right after [[renderFile()]] renders a view file.
  33. * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
  34. * 后置事件,在执行[renderFile()]方法后被调用,默认触发[[EVENT_AFTER_RENDER]]事件
  35. * If you override this method, make sure you call the parent implementation first.
  36. * @param string $viewFile the view file being rendered.要渲染的视图文件。
  37. * @param array $params the parameter array passed to the [[render()]] method.
  38. * 参数数组传递到[render()]方法。
  39. * @param string $output the rendering result of the view file. Updates to this parameter
  40. * will be passed back and returned by [[renderFile()]].
  41. * 返回视图渲染的结果
  42. */
  43. public function afterRender($viewFile, $params, &$output)
  44. {
  45. if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {//判断[EVENT_AFTER_RENDER]事件是否存在
  46. $event = new ViewEvent([
  47. 'viewFile' => $viewFile,
  48. 'params' => $params,
  49. 'output' => $output,
  50. ]);
  51. //触发[EVENT_AFTER_RENDER]事件
  52. $this->trigger(self::EVENT_AFTER_RENDER, $event);
  53. $output = $event->output;//返回结果
  54. }
  55. }
  56.  
  57. /**
  58. * Renders a view file as a PHP script.
  59. * 返回一个视图文件当作PHP脚本
  60. * This method treats the view file as a PHP script and includes the file.
  61. * It extracts the given parameters and makes them available in the view file.
  62. * The method captures the output of the included view file and returns it as a string.
  63. * 将传入的参数转换为变量,包含并执行view文件,返回执行结果
  64. * This method should mainly be called by view renderer or [[renderFile()]].
  65. *
  66. * @param string $_file_ the view file. 视图文件
  67. * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
  68. * @return string the rendering result 执行结果
  69. */
  70. public function renderPhpFile($_file_, $_params_ = [])
  71. {
  72. ob_start(); //打开输出缓冲
  73. ob_implicit_flush(false); //关闭缓冲区
  74. extract($_params_, EXTR_OVERWRITE);// 将一个数组转换为变量使用
  75. require($_file_);
  76.  
  77. return ob_get_clean();//得到缓冲区的内容并清除当前输出缓冲
  78. }
  79.  
  80. /**
  81. * Renders dynamic content returned by the given PHP statements. 渲染动态内容
  82. * This method is mainly used together with content caching (fragment caching and page caching)
  83. * 用来聚合缓存的内容
  84. * when some portions of the content (called *dynamic content*) should not be cached.
  85. * The dynamic content must be returned by some PHP statements.
  86. * 渲染某些被PHP语句返回的动态内容
  87. * @param string $statements the PHP statements for generating the dynamic content.生成动态内容的PHP语句。
  88. * @return string the placeholder of the dynamic content, or the dynamic content if there is no
  89. * active content cache currently. 动态内容占位符 如果当前没有有效的内容缓存,调用evaluateDynamicContent输出
  90. */
  91. public function renderDynamic($statements)
  92. {
  93. if (!empty($this->cacheStack)) {//动态内容的列表不为空
  94. $n = count($this->dynamicPlaceholders);//统计动态内容条数
  95. $placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";//生成占位符
  96. $this->addDynamicPlaceholder($placeholder, $statements);//添加动态内容占位符
  97.  
  98. return $placeholder;
  99. } else {//没有有效缓存 执行传入的PHP语句,返回执行结果
  100. return $this->evaluateDynamicContent($statements);
  101. }
  102. }
  103.  
  104. /**
  105. * Adds a placeholder for dynamic content. 添加一个动态内容占位符
  106. * This method is internally used. 内部使用
  107. * @param string $placeholder the placeholder name 占位符名称
  108. * @param string $statements the PHP statements for generating the dynamic content
  109. * 生成动态内容的PHP语句
  110. */
  111. public function addDynamicPlaceholder($placeholder, $statements)
  112. {
  113. foreach ($this->cacheStack as $cache) {
  114. $cache->dynamicPlaceholders[$placeholder] = $statements;//添加动态内容占位符
  115. }
  116. $this->dynamicPlaceholders[$placeholder] = $statements;//给当前视图添加动态内容占位符
  117. }
  118.  
  119. /**
  120. * Evaluates the given PHP statements. 给定的PHP语句的值
  121. * This method is mainly used internally to implement dynamic content feature.内部使用实现动态内容功能
  122. * @param string $statements the PHP statements to be evaluated. PHP语句进行计算
  123. * @return mixed the return value of the PHP statements. PHP语句的值
  124. */
  125. public function evaluateDynamicContent($statements)
  126. {
  127. return eval($statements);
  128. }
  129.  
  130. /**
  131. * Begins recording a block.
  132. * This method is a shortcut to beginning [[Block]]
  133. * 数据块开始的标记,该方法是开始[Block]的快捷方式
  134. * 数据块可以在一个地方指定视图内容在另一个地方显示,通常和布局一起使用
  135. * @param string $id the block ID. 数据块标识
  136. * @param boolean $renderInPlace whether to render the block content in place. 是否渲染块内容。
  137. * Defaults to false, meaning the captured block will not be displayed.
  138. * @return Block the Block widget instance 数据块部件实例
  139. */
  140. public function beginBlock($id, $renderInPlace = false)
  141. {
  142. return Block::begin([
  143. 'id' => $id,//数据块唯一标识
  144. 'renderInPlace' => $renderInPlace,//是否显示标识
  145. 'view' => $this,
  146. ]);
  147. }
  148.  
  149. /**
  150. * Ends recording a block. 数据块结束标识
  151. */
  152. public function endBlock()
  153. {
  154. Block::end();
  155. }
  156.  
  157. /**
  158. * Begins the rendering of content that is to be decorated by the specified view.
  159. * This method can be used to implement nested layout. For example, a layout can be embedded
  160. * in another layout file specified as '@app/views/layouts/base.php' like the following:
  161. * 开始指定的view渲染内容,用来实现嵌套布局,传入的第一个参数为布局文件的路径
  162. * ~~~
  163. * <?php $this->beginContent('@app/views/layouts/base.php'); ?>
  164. * ...layout content here...
  165. * <?php $this->endContent(); ?>
  166. * ~~~
  167. *
  168. * @param string $viewFile the view file that will be used to decorate the content enclosed by this widget.
  169. * This can be specified as either the view file path or path alias.布局文件的路径或路径别名。
  170. * @param array $params the variables (name => value) to be extracted and made available in the decorative view.
  171. * 可以在视图中运用的参数
  172. * @return ContentDecorator the ContentDecorator widget instance 部件实例
  173. * @see ContentDecorator
  174. */
  175. public function beginContent($viewFile, $params = [])
  176. {
  177. return ContentDecorator::begin([
  178. 'viewFile' => $viewFile,
  179. 'params' => $params,
  180. 'view' => $this,
  181. ]);
  182. }
  183.  
  184. /**
  185. * Ends the rendering of content.结束渲染内容
  186. */
  187. public function endContent()
  188. {
  189. ContentDecorator::end();
  190. }
  191.  
  192. /**
  193. * Begins fragment caching. 开始片段缓存
  194. * This method will display cached content if it is available.
  195. * If not, it will start caching and would expect an [[endCache()]]
  196. * call to end the cache and save the content into cache.
  197. * 展示可用的缓存内容,否则将开始缓存内容直到出现[endCache()]方法
  198. * A typical usage of fragment caching is as follows,
  199. *
  200. * ~~~
  201. * if ($this->beginCache($id)) {
  202. * // ...generate content here
  203. * $this->endCache();
  204. * }
  205. * ~~~
  206. *
  207. * @param string $id a unique ID identifying the fragment to be cached.缓存片段的唯一标识
  208. * @param array $properties initial property values for [[FragmentCache]]初始属性[FragmentCache]
  209. * @return boolean whether you should generate the content for caching. 是否生成缓存的内容。
  210. * False if the cached version is available.
  211. */
  212. public function beginCache($id, $properties = [])
  213. {
  214. $properties['id'] = $id; //片段标识
  215. $properties['view'] = $this; //调用初始化属性
  216. /* @var $cache FragmentCache */
  217. $cache = FragmentCache::begin($properties);
  218. if ($cache->getCachedContent() !== false) {
  219. $this->endCache();//从缓存中读取到了缓存的内容,则渲染内容并返回 false,不再进行缓存
  220.  
  221. return false;
  222. } else {
  223. return true;
  224. }
  225. }
  226.  
  227. /**
  228. * Ends fragment caching. 结束片段缓存
  229. */
  230. public function endCache()
  231. {
  232. FragmentCache::end();
  233. }
  234.  
  235. /**
  236. * Marks the beginning of a page.页面开始标记
  237. */
  238. public function beginPage()
  239. {
  240. ob_start(); //打开输出缓冲
  241. ob_implicit_flush(false);//关闭缓冲区
  242.  
  243. $this->trigger(self::EVENT_BEGIN_PAGE);
  244. }
  245.  
  246. /**
  247. * Marks the ending of a page. 页面结束标记
  248. */
  249. public function endPage()
  250. {
  251. $this->trigger(self::EVENT_END_PAGE);
  252. ob_end_flush();//关闭输出缓冲区
  253. }

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. C# .Net基础知识点解答

    原文地址 1. 什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式? 通俗的讲,.Net是微软开发应用程序的一个平台: CLI是C ...

  2. Nodejs实现代理服务器配置

    var net = require('net'); var local_port = 8893; //在本地创建一个server监听本地local_port端口 net.createServer(fu ...

  3. Hyper-V虚拟化--逻辑网络、VM网络、逻辑交换机

    逻辑网络承接物理网卡和VM网卡 可以关联站点.主机组.VLAN.IP子网,配置静态IP地址池(虚机的PA地址从该静态IP地址池获取) 只有当逻辑网络中的网络站点关联了VLAN后,在VM中才可以选择VL ...

  4. Hyper-V虚机跨NUMA节点性能影响

    查看单个NUMA节点的内存大小: Hyper-V VM VID NUMA节点\PageCount:83769708376970*4k/1024/1024=32GB Hyper-V VM VID NUM ...

  5. Entity Framework 教程(转)

    预备知识    2 LINQ技术    2 LINQ技术的基础 - C#3.0    2 自动属性    2 隐式类型    2 对象初始化器与集合初始化器    3 匿名类    3 扩展方法    ...

  6. 关于着色器LinearGradient的使用

    LinearGradient我们可以将之译为线型渐变.线型渲染等,译成什么不重要,重要的是它的显示效果是什么样子,今天我们就一起来看看. 先来看看LinearGradient的构造方法: /** Cr ...

  7. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(五)

    这是本系列的最后一篇,主要讲一下FreeMarker模板引擎的基本概念与常用指令的使用方式.     一.FreemMarker基本概念     FreemMarker是一个用Java语言编写的模板引 ...

  8. 开源安全测试 - BackTrack的应用

    开源安全测试 - BackTrack的应用: http://gdtesting.com/product.php?id=99  

  9. case when then 与 count联合使用

    我们都知道SQL中适用case when then来转化数据库中的信息 比如  select (case sex when 0 then '男' else '女' end) AS sex  from ...

  10. nyoj 33 蛇形填数

    蛇形填数 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 ...