Yii源码阅读笔记(二十)

View中应用布局和缓存内容部分:

  1   /**
  2      * Begins recording a block.
  3      * This method is a shortcut to beginning [[Block]]
  4      * 数据块开始的标记,该方法是开始[Block]的快捷方式
  5      * 数据块可以在一个地方指定视图内容在另一个地方显示,通常和布局一起使用
  6      * @param string $id the block ID.
  7      * @param boolean $renderInPlace whether to render the block content in place.
  8      * Defaults to false, meaning the captured block will not be displayed.
  9      * @return Block the Block widget instance
 10      */
 11     public function beginBlock($id, $renderInPlace = false)
 12     {
 13         return Block::begin([
 14             ‘id‘ => $id,//数据块唯一标识
 15             ‘renderInPlace‘ => $renderInPlace,//是否显示标记,默认false不显示
 16             ‘view‘ => $this,
 17         ]);
 18     }
 19
 20     /**
 21      * Ends recording a block.
 22      * 数据块结束的标记
 23      */
 24     public function endBlock()
 25     {
 26         Block::end();
 27     }
 28
 29     /**
 30      * Begins the rendering of content that is to be decorated by the specified view.
 31      * 开始用指定的view渲染内容,该方法用来实现嵌套布局,传入的第一个参数为布局文件的路径
 32      * This method can be used to implement nested layout. For example, a layout can be embedded
 33      * in another layout file specified as ‘@app/views/layouts/base.php‘ like the following:
 34      *
 35      * ```php
 36      * <?php $this->beginContent(‘@app/views/layouts/base.php‘); ?>
 37      * //...layout content here...
 38      * <?php $this->endContent(); ?>
 39      * ```
 40      *
 41      * @param string $viewFile the view file that will be used to decorate the content enclosed by this widget.
 42      * This can be specified as either the view file path or path alias.
 43      * @param string $viewFile 布局文件
 44      * @param array $params the variables (name => value) to be extracted and made available in the decorative view.
 45      * @param array $params 布局view文件的参数
 46      * @return ContentDecorator the ContentDecorator widget instance
 47      * @see ContentDecorator
 48      */
 49     public function beginContent($viewFile, $params = [])
 50     {
 51         return ContentDecorator::begin([
 52             ‘viewFile‘ => $viewFile,
 53             ‘params‘ => $params,
 54             ‘view‘ => $this,
 55         ]);
 56     }
 57
 58     /**
 59      * Ends the rendering of content.
 60      * 结束渲染内容
 61      */
 62     public function endContent()
 63     {
 64         ContentDecorator::end();
 65     }
 66
 67     /**
 68      * Begins fragment caching.
 69      * 开始片段缓存
 70      * This method will display cached content if it is available.
 71      * 该方法将展示可用的缓存内容,否则将开始缓存内容直到出现[endCache()]方法
 72      * If not, it will start caching and would expect an [[endCache()]]
 73      * call to end the cache and save the content into cache.
 74      * A typical usage of fragment caching is as follows,
 75      * 使用缓存的典型例子如下:
 76      * ```php
 77      * if ($this->beginCache($id)) {
 78      *     // ...generate content here
 79      *     $this->endCache();
 80      * }
 81      * ```
 82      *
 83      * @param string $id a unique ID identifying the fragment to be cached.
 84      * @param array $properties initial property values for [[FragmentCache]]
 85      * @return boolean whether you should generate the content for caching.
 86      * False if the cached version is available.
 87      */
 88     public function beginCache($id, $properties = [])
 89     {
 90         $properties[‘id‘] = $id;//缓存唯一标识
 91         $properties[‘view‘] = $this;
 92         /* @var $cache FragmentCache */
 93         $cache = FragmentCache::begin($properties);
 94         if ($cache->getCachedContent() !== false) {//如果从缓存中读取到了缓存的内容,则渲染内容并返回 false,因此将跳过内容生成逻辑,不再进行缓存
 95             $this->endCache();
 96
 97             return false;
 98         } else {
 99             return true;
100         }
101     }
102
103     /**
104      * Ends fragment caching.
105      * 结束片段缓存
106      */
107     public function endCache()
108     {
109         FragmentCache::end();
110     }
111
112     /**
113      * Marks the beginning of a page.
114      * 页面开始标记--用于生成页面布局文件
115      */
116     public function beginPage()
117     {
118         ob_start();
119         ob_implicit_flush(false);
120
121         $this->trigger(self::EVENT_BEGIN_PAGE);
122     }
123
124     /**
125      * Marks the ending of a page.
126      * 页面结束标记--用于生成页面布局文件
127      */
128     public function endPage()
129     {
130         $this->trigger(self::EVENT_END_PAGE);
131         ob_end_flush();
132     }
时间: 2025-01-13 11:23:02

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

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

Yii源码阅读笔记(十八)

View中的查找视图文件方法和渲染文件方法 1 /** 2 * Finds the view file based on the given view name. 3 * 通过view文件名查找view文件 4 * @param string $view the view name or the path alias of the view file. Please refer to [[render()]] 5 * on how to specify this parameter. 6 * @

Yii源码阅读笔记(十五)

Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mainly used for display purpose. For example, given an attribute * `firstName`, we can declare a label `First Name` which is more user-friendly and can *

Yii源码阅读笔记(十四)

Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: 1 /** 2 * Returns a list of scenarios and the corresponding active attributes. 3 * An active attribute is one that is subject to validation in the current scenario. 4 * 返回所有场景及与之对应的 active 属性的列表 5 * active 属性是指在默认场景中验

Yii源码阅读笔记(十六)

Model类,集中整个应用的数据和业务逻辑—— /** * Generates a user friendly attribute label based on the give attribute name. * 生成一个对用户友好的属性标签,将属性名中的下划线.破折号.点替换为空格,并且每个单词的首字母大写 * This is done by replacing underscores, dashes and dots with blanks and * changing the first

Yii源码阅读笔记(十)

控制器类,所有控制器的基类,用于调用模型和布局,输出到视图 1 namespace yii\base; 2 3 use Yii; 4 5 /** 6 * Controller is the base class for classes containing controller logic. 7 * 控制器,是所用控制器类的基类 8 * 9 * @property Module[] $modules 只读属性 当前控制器的所有模块 10 * 11 * @property string $rout

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

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

Java Jdk1.8 HashMap源码阅读笔记二

三.源码阅读 3.元素包含containsKey(Object key) /** * Returns <tt>true</tt> if this map contains a mapping for the * specified key. * * @param key The key whose presence in this map is to be tested * @return <tt>true</tt> if this map contains

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