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 }