码迷,mamicode.com
首页 > 其他好文 > 详细

Yii源码阅读笔记(十七)

时间:2016-04-26 01:50:22      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

View.php,继承了component,用于渲染视图文件:

  1 namespace yii\base;
  2 
  3 use Yii;
  4 use yii\helpers\FileHelper;
  5 use yii\widgets\Block;
  6 use yii\widgets\ContentDecorator;
  7 use yii\widgets\FragmentCache;
  8 
  9 /**
 10  * View represents a view object in the MVC pattern.
 11  *
 12  * View provides a set of methods (e.g. [[render()]]) for rendering purpose.
 13  * 视图提供了一套渲染页面的方法(例如[render()])
 14  *
 15  * @property string|boolean $viewFile The view file currently being rendered. False if no view file is being
 16  * rendered. This property is read-only.
 17  *
 18  * @author Qiang Xue <qiang.xue@gmail.com>
 19  * @since 2.0
 20  */
 21 class View extends Component
 22 {
 23     /**
 24      * @event Event an event that is triggered by [[beginPage()]].
 25      * beginPage事件名常量,被[beginPage()]触发
 26      */
 27     const EVENT_BEGIN_PAGE = ‘beginPage‘;
 28     /**
 29      * @event Event an event that is triggered by [[endPage()]].
 30      * endPage事件名常量,被[endPage()]触发
 31      */
 32     const EVENT_END_PAGE = ‘endPage‘;
 33     /**
 34      * @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file.
 35      * beforeRender事件名常量,被[renderFile()]触发
 36      */
 37     const EVENT_BEFORE_RENDER = ‘beforeRender‘;
 38     /**
 39      * @event ViewEvent an event that is triggered by [[renderFile()]] right after it renders a view file.
 40      * afterRender事件名常量,被[renderFile()]触发
 41      */
 42     const EVENT_AFTER_RENDER = ‘afterRender‘;
 43 
 44     /**
 45      * @var ViewContextInterface the context under which the [[renderFile()]] method is being invoked.
 46      */
 47     public $context;
 48     /**
 49      * @var mixed custom parameters that are shared among view templates.
 50      * 在视图模板中共享的自定义参数
 51      */
 52     public $params = [];
 53     /**
 54      * @var array a list of available renderers indexed by their corresponding supported file extensions.
 55      * Each renderer may be a view renderer object or the configuration for creating the renderer object.
 56      * 一个可用的渲染索引列表。每个渲染器是一个渲染器对象或创建渲染对象配置数组
 57      * For example, the following configuration enables both Smarty and Twig view renderers:
 58      * 下面的配置数组同时支持 Smarty 和 Twig 
 59      *
 60      * ```php
 61      * [
 62      *     ‘tpl‘ => [‘class‘ => ‘yii\smarty\ViewRenderer‘],
 63      *     ‘twig‘ => [‘class‘ => ‘yii\twig\ViewRenderer‘],
 64      * ]
 65      * ```
 66      *
 67      * If no renderer is available for the given view file, the view file will be treated as a normal PHP
 68      * and rendered via [[renderPhpFile()]].
 69      * 如果没有值,则
 70      */
 71     public $renderers;
 72     /**
 73      * @var string the default view file extension. This will be appended to view file names if they don‘t have file extensions.
 74      * 默认视图文件扩展名,在视图文件没有扩展名的情况下自动添加
 75      */
 76     public $defaultExtension = ‘php‘;
 77     /**
 78      * @var Theme|array|string the theme object or the configuration for creating the theme object.
 79      * If not set, it means theming is not enabled.
 80      * 主题对象或创建主题对象的配置 
 81      */
 82     public $theme;
 83     /**
 84      * @var array a list of named output blocks. The keys are the block names and the values
 85      * are the corresponding block content. You can call [[beginBlock()]] and [[endBlock()]]
 86      * to capture small fragments of a view. They can be later accessed somewhere else
 87      * through this property.
 88      * 一个输出块列表。键是块名称值为相应的块内容。你可以调用 [ beginblock() ]和[ endblock() ]捕获视图的small fragments
 89      * 它们可以在其他地方通过这个属性访问。
 90      */
 91     public $blocks;
 92     /**
 93      * @var array a list of currently active fragment cache widgets. This property
 94      * is used internally to implement the content caching feature. Do not modify it directly.
 95      * 当前active fragment缓存小部件的列表。此属性用于内部实现内容缓存功能。不要直接修改它。
 96      * @internal
 97      */
 98     public $cacheStack = [];
 99     /**
100      * @var array a list of placeholders for embedding dynamic contents. This property
101      * is used internally to implement the content caching feature. Do not modify it directly.
102      * 一种嵌入动态内容占位符列表。此属性用于内部实现内容缓存功能。不要直接修改它。
103      * @internal
104      */
105     public $dynamicPlaceholders = [];
106 
107     /**
108      * @var array the view files currently being rendered. There may be multiple view files being
109      * rendered at a moment because one view may be rendered within another.
110      * 目前正在渲染的视图文件。可能有多个视图文件被渲染,因为一个视图可以在另一个视图中呈现
111      */
112     private $_viewFiles = [];
113 
114 
115     /**
116      * Initializes the view component.
117      * 初始化视图组件
118      */
119     public function init()
120     {
121         parent::init();//调用父类的init方法
122         if (is_array($this->theme)) {//如果theme是数组
123             if (!isset($this->theme[‘class‘])) {//且没有设置类名
124                 $this->theme[‘class‘] = ‘yii\base\Theme‘;//则类名为‘yii\base\Theme‘
125             }
126             //以配置的形式创建对象
127             $this->theme = Yii::createObject($this->theme);
128         } elseif (is_string($this->theme)) {//否则以字符串参数的形式创建
129             $this->theme = Yii::createObject($this->theme);
130         }
131     }
132 
133     /**
134      * Renders a view.
135      * 渲染一个视图
136      * The view to be rendered can be specified in one of the following formats:
137      * 被渲染的视图可以用下列方式指定
138      *
139      * - path alias (e.g. "@app/views/site/index");
140      *   路径别名
141      * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
142      *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
143      *   绝对路径,将会在[Application::viewPath|view path]下查找文件
144      * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash.
145      *   The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]].
146      *   模块下的绝对路径,将会在[Module::viewPath|view path]下查找文件
147      * - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be
148      *   looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`.
149      *   相对路径,将会在[ViewContextInterface::getViewPath()|view path]下查找文件
150      *   If `$context` is not given, it will be looked for under the directory containing the view currently
151      *   being rendered (i.e., this happens when rendering a view within another view).
152      *
153      * @param string $view the view name.
154      * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
155      * @param object $context the context to be assigned to the view and can later be accessed via [[context]]
156      * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate
157      * the view file corresponding to a relative view name.
158      * @return string the rendering result
159      * @throws InvalidParamException if the view cannot be resolved or the view file does not exist.
160      * @see renderFile()
161      */
162     public function render($view, $params = [], $context = null)
163     {
164         //查找视图文件路径
165         $viewFile = $this->findViewFile($view, $context);
166         //调用renderFile()渲染视图文件
167         return $this->renderFile($viewFile, $params, $context);
168     }

 

Yii源码阅读笔记(十七)

标签:

原文地址:http://www.cnblogs.com/isleep/p/5433381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!