1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\base; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\helpers\FileHelper; |
12
|
|
|
use yii\widgets\Block; |
13
|
|
|
use yii\widgets\ContentDecorator; |
14
|
|
|
use yii\widgets\FragmentCache; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* View represents a view object in the MVC pattern. |
18
|
|
|
* |
19
|
|
|
* View provides a set of methods (e.g. [[render()]]) for rendering purpose. |
20
|
|
|
* |
21
|
|
|
* For more details and usage information on View, see the [guide article on views](guide:structure-views). |
22
|
|
|
* |
23
|
|
|
* @property string|bool $viewFile The view file currently being rendered. False if no view file is being |
24
|
|
|
* rendered. This property is read-only. |
25
|
|
|
* |
26
|
|
|
* @author Qiang Xue <[email protected]> |
27
|
|
|
* @since 2.0 |
28
|
|
|
*/ |
29
|
|
|
class View extends Component implements DynamicContentAwareInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @event Event an event that is triggered by [[beginPage()]]. |
33
|
|
|
*/ |
34
|
|
|
const EVENT_BEGIN_PAGE = 'beginPage'; |
35
|
|
|
/** |
36
|
|
|
* @event Event an event that is triggered by [[endPage()]]. |
37
|
|
|
*/ |
38
|
|
|
const EVENT_END_PAGE = 'endPage'; |
39
|
|
|
/** |
40
|
|
|
* @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file. |
41
|
|
|
*/ |
42
|
|
|
const EVENT_BEFORE_RENDER = 'beforeRender'; |
43
|
|
|
/** |
44
|
|
|
* @event ViewEvent an event that is triggered by [[renderFile()]] right after it renders a view file. |
45
|
|
|
*/ |
46
|
|
|
const EVENT_AFTER_RENDER = 'afterRender'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ViewContextInterface the context under which the [[renderFile()]] method is being invoked. |
50
|
|
|
*/ |
51
|
|
|
public $context; |
52
|
|
|
/** |
53
|
|
|
* @var mixed custom parameters that are shared among view templates. |
54
|
|
|
*/ |
55
|
|
|
public $params = []; |
56
|
|
|
/** |
57
|
|
|
* @var array a list of available renderers indexed by their corresponding supported file extensions. |
58
|
|
|
* Each renderer may be a view renderer object or the configuration for creating the renderer object. |
59
|
|
|
* For example, the following configuration enables both Smarty and Twig view renderers: |
60
|
|
|
* |
61
|
|
|
* ```php |
62
|
|
|
* [ |
63
|
|
|
* 'tpl' => ['__class' => \yii\smarty\ViewRenderer::class], |
64
|
|
|
* 'twig' => ['__class' => \yii\twig\ViewRenderer::class], |
65
|
|
|
* ] |
66
|
|
|
* ``` |
67
|
|
|
* |
68
|
|
|
* If no renderer is available for the given view file, the view file will be treated as a normal PHP |
69
|
|
|
* and rendered via [[renderPhpFile()]]. |
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
|
|
|
public $defaultExtension = 'php'; |
76
|
|
|
/** |
77
|
|
|
* @var Theme|array|string the theme object or the configuration for creating the theme object. |
78
|
|
|
* If not set, it means theming is not enabled. |
79
|
|
|
*/ |
80
|
|
|
public $theme; |
81
|
|
|
/** |
82
|
|
|
* @var array a list of named output blocks. The keys are the block names and the values |
83
|
|
|
* are the corresponding block content. You can call [[beginBlock()]] and [[endBlock()]] |
84
|
|
|
* to capture small fragments of a view. They can be later accessed somewhere else |
85
|
|
|
* through this property. |
86
|
|
|
*/ |
87
|
|
|
public $blocks; |
88
|
|
|
/** |
89
|
|
|
* @var DynamicContentAwareInterface[] a list of currently active dynamic content class instances. |
90
|
|
|
*/ |
91
|
|
|
private $_cacheStack = []; |
92
|
|
|
/** |
93
|
|
|
* @var array a list of placeholders for embedding dynamic contents. |
94
|
|
|
*/ |
95
|
|
|
private $_dynamicPlaceholders = []; |
96
|
|
|
/** |
97
|
|
|
* @var array the view files currently being rendered. There may be multiple view files being |
98
|
|
|
* rendered at a moment because one view may be rendered within another. |
99
|
|
|
*/ |
100
|
|
|
private $_viewFiles = []; |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Initializes the view component. |
105
|
|
|
*/ |
106
|
152 |
|
public function init() |
107
|
|
|
{ |
108
|
152 |
|
parent::init(); |
109
|
152 |
|
if (is_array($this->theme)) { |
110
|
|
|
if (!isset($this->theme['__class'])) { |
111
|
|
|
$this->theme['__class'] = Theme::class; |
112
|
|
|
} |
113
|
|
|
$this->theme = Yii::createObject($this->theme); |
114
|
152 |
|
} elseif (is_string($this->theme)) { |
115
|
|
|
$this->theme = Yii::createObject($this->theme); |
116
|
|
|
} |
117
|
152 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Renders a view. |
121
|
|
|
* |
122
|
|
|
* The view to be rendered can be specified in one of the following formats: |
123
|
|
|
* |
124
|
|
|
* - [path alias](guide:concept-aliases) (e.g. "@app/views/site/index"); |
125
|
|
|
* - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. |
126
|
|
|
* The actual view file will be looked for under the [[Application::viewPath|view path]] of the application. |
127
|
|
|
* - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash. |
128
|
|
|
* The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]]. |
129
|
|
|
* - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be |
130
|
|
|
* looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`. |
131
|
|
|
* If `$context` is not given, it will be looked for under the directory containing the view currently |
132
|
|
|
* being rendered (i.e., this happens when rendering a view within another view). |
133
|
|
|
* |
134
|
|
|
* @param string $view the view name. |
135
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
136
|
|
|
* @param object $context the context to be assigned to the view and can later be accessed via [[context]] |
137
|
|
|
* in the view. If the context implements [[ViewContextInterface]], it may also be used to locate |
138
|
|
|
* the view file corresponding to a relative view name. |
139
|
|
|
* @return string the rendering result |
140
|
|
|
* @throws ViewNotFoundException if the view file does not exist. |
141
|
|
|
* @throws InvalidCallException if the view cannot be resolved. |
142
|
|
|
* @see renderFile() |
143
|
|
|
*/ |
144
|
37 |
|
public function render($view, $params = [], $context = null) |
145
|
|
|
{ |
146
|
37 |
|
$viewFile = $this->findViewFile($view, $context); |
147
|
37 |
|
return $this->renderFile($viewFile, $params, $context); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Finds the view file based on the given view name. |
152
|
|
|
* @param string $view the view name or the [path alias](guide:concept-aliases) of the view file. Please refer to [[render()]] |
153
|
|
|
* on how to specify this parameter. |
154
|
|
|
* @param object $context the context to be assigned to the view and can later be accessed via [[context]] |
155
|
|
|
* in the view. If the context implements [[ViewContextInterface]], it may also be used to locate |
156
|
|
|
* the view file corresponding to a relative view name. |
157
|
|
|
* @return string the view file path. Note that the file may not exist. |
158
|
|
|
* @throws InvalidCallException if a relative view name is given while there is no active context to |
159
|
|
|
* determine the corresponding view file. |
160
|
|
|
*/ |
161
|
37 |
|
protected function findViewFile($view, $context = null) |
162
|
|
|
{ |
163
|
37 |
|
if (strncmp($view, '@', 1) === 0) { |
164
|
|
|
// e.g. "@app/views/main" |
165
|
25 |
|
$file = Yii::getAlias($view); |
166
|
13 |
|
} elseif (strncmp($view, '//', 2) === 0) { |
167
|
|
|
// e.g. "//layouts/main" |
168
|
|
|
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/'); |
169
|
13 |
|
} elseif (strncmp($view, '/', 1) === 0) { |
170
|
|
|
// e.g. "/site/index" |
171
|
|
|
if (Yii::$app->controller !== null) { |
172
|
|
|
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/'); |
173
|
|
|
} else { |
174
|
|
|
throw new InvalidCallException("Unable to locate view file for view '$view': no active controller."); |
175
|
|
|
} |
176
|
13 |
|
} elseif ($context instanceof ViewContextInterface) { |
177
|
7 |
|
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view; |
178
|
6 |
|
} elseif (($currentViewFile = $this->getRequestedViewFile()) !== false) { |
179
|
6 |
|
$file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view; |
180
|
|
|
} else { |
181
|
|
|
throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context."); |
182
|
|
|
} |
183
|
|
|
|
184
|
37 |
|
if (pathinfo($file, PATHINFO_EXTENSION) !== '') { |
185
|
23 |
|
return $file; |
186
|
|
|
} |
187
|
14 |
|
$path = $file . '.' . $this->defaultExtension; |
188
|
14 |
|
if ($this->defaultExtension !== 'php' && !is_file($path)) { |
189
|
|
|
$path = $file . '.php'; |
190
|
|
|
} |
191
|
|
|
|
192
|
14 |
|
return $path; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Renders a view file. |
197
|
|
|
* |
198
|
|
|
* If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long |
199
|
|
|
* as it is available. |
200
|
|
|
* |
201
|
|
|
* The method will call [[FileHelper::localize()]] to localize the view file. |
202
|
|
|
* |
203
|
|
|
* If [[renderers|renderer]] is enabled (not null), the method will use it to render the view file. |
204
|
|
|
* Otherwise, it will simply include the view file as a normal PHP file, capture its output and |
205
|
|
|
* return it as a string. |
206
|
|
|
* |
207
|
|
|
* @param string $viewFile the view file. This can be either an absolute file path or an alias of it. |
208
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
209
|
|
|
* @param object $context the context that the view should use for rendering the view. If null, |
210
|
|
|
* existing [[context]] will be used. |
211
|
|
|
* @return string the rendering result |
212
|
|
|
* @throws ViewNotFoundException if the view file does not exist |
213
|
|
|
*/ |
214
|
77 |
|
public function renderFile($viewFile, $params = [], $context = null) |
215
|
|
|
{ |
216
|
77 |
|
$viewFile = $requestedFile = Yii::getAlias($viewFile); |
217
|
|
|
|
218
|
77 |
|
if ($this->theme !== null) { |
219
|
1 |
|
$viewFile = $this->theme->applyTo($viewFile); |
220
|
|
|
} |
221
|
77 |
|
if (is_file($viewFile)) { |
222
|
76 |
|
$viewFile = FileHelper::localize($viewFile); |
223
|
|
|
} else { |
224
|
2 |
|
throw new ViewNotFoundException("The view file does not exist: $viewFile"); |
225
|
|
|
} |
226
|
|
|
|
227
|
76 |
|
$oldContext = $this->context; |
228
|
76 |
|
if ($context !== null) { |
229
|
26 |
|
$this->context = $context; |
230
|
|
|
} |
231
|
76 |
|
$output = ''; |
232
|
76 |
|
$this->_viewFiles[] = [ |
233
|
76 |
|
'resolved' => $viewFile, |
234
|
76 |
|
'requested' => $requestedFile |
235
|
|
|
]; |
236
|
|
|
|
237
|
76 |
|
if ($this->beforeRender($viewFile, $params)) { |
238
|
76 |
|
Yii::debug("Rendering view file: $viewFile", __METHOD__); |
239
|
76 |
|
$ext = pathinfo($viewFile, PATHINFO_EXTENSION); |
240
|
76 |
|
if (isset($this->renderers[$ext])) { |
241
|
|
|
if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) { |
242
|
|
|
$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]); |
243
|
|
|
} |
244
|
|
|
/* @var $renderer ViewRenderer */ |
245
|
|
|
$renderer = $this->renderers[$ext]; |
246
|
|
|
$output = $renderer->render($this, $viewFile, $params); |
247
|
|
|
} else { |
248
|
76 |
|
$output = $this->renderPhpFile($viewFile, $params); |
249
|
|
|
} |
250
|
76 |
|
$this->afterRender($viewFile, $params, $output); |
251
|
|
|
} |
252
|
|
|
|
253
|
76 |
|
array_pop($this->_viewFiles); |
254
|
76 |
|
$this->context = $oldContext; |
255
|
|
|
|
256
|
76 |
|
return $output; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return string|bool the view file currently being rendered. False if no view file is being rendered. |
261
|
|
|
*/ |
262
|
|
|
public function getViewFile() |
263
|
|
|
{ |
264
|
|
|
return empty($this->_viewFiles) ? false : end($this->_viewFiles)['resolved']; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return string|bool the requested view currently being rendered. False if no view file is being rendered. |
269
|
|
|
* @since 2.0.16 |
270
|
|
|
*/ |
271
|
6 |
|
protected function getRequestedViewFile() |
272
|
|
|
{ |
273
|
6 |
|
return empty($this->_viewFiles) ? false : end($this->_viewFiles)['requested']; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* This method is invoked right before [[renderFile()]] renders a view file. |
278
|
|
|
* The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event. |
279
|
|
|
* If you override this method, make sure you call the parent implementation first. |
280
|
|
|
* @param string $viewFile the view file to be rendered. |
281
|
|
|
* @param array $params the parameter array passed to the [[render()]] method. |
282
|
|
|
* @return bool whether to continue rendering the view file. |
283
|
|
|
*/ |
284
|
76 |
|
public function beforeRender($viewFile, $params) |
285
|
|
|
{ |
286
|
76 |
|
$event = new ViewEvent([ |
287
|
76 |
|
'name' => self::EVENT_BEFORE_RENDER, |
288
|
76 |
|
'viewFile' => $viewFile, |
289
|
76 |
|
'params' => $params, |
290
|
|
|
]); |
291
|
76 |
|
$this->trigger($event); |
292
|
|
|
|
293
|
76 |
|
return $event->isValid; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* This method is invoked right after [[renderFile()]] renders a view file. |
298
|
|
|
* The default implementation will trigger the [[EVENT_AFTER_RENDER]] event. |
299
|
|
|
* If you override this method, make sure you call the parent implementation first. |
300
|
|
|
* @param string $viewFile the view file being rendered. |
301
|
|
|
* @param array $params the parameter array passed to the [[render()]] method. |
302
|
|
|
* @param string $output the rendering result of the view file. Updates to this parameter |
303
|
|
|
* will be passed back and returned by [[renderFile()]]. |
304
|
|
|
*/ |
305
|
76 |
|
public function afterRender($viewFile, $params, &$output) |
306
|
|
|
{ |
307
|
76 |
|
if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) { |
308
|
|
|
$event = new ViewEvent([ |
309
|
|
|
'name' => self::EVENT_BEFORE_RENDER, |
310
|
|
|
'viewFile' => $viewFile, |
311
|
|
|
'params' => $params, |
312
|
|
|
'output' => $output, |
313
|
|
|
]); |
314
|
|
|
$this->trigger($event); |
315
|
|
|
$output = $event->output; |
316
|
|
|
} |
317
|
76 |
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Renders a view file as a PHP script. |
321
|
|
|
* |
322
|
|
|
* This method treats the view file as a PHP script and includes the file. |
323
|
|
|
* It extracts the given parameters and makes them available in the view file. |
324
|
|
|
* The method captures the output of the included view file and returns it as a string. |
325
|
|
|
* |
326
|
|
|
* This method should mainly be called by view renderer or [[renderFile()]]. |
327
|
|
|
* |
328
|
|
|
* @param string $_file_ the view file. |
329
|
|
|
* @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
330
|
|
|
* @return string the rendering result |
331
|
|
|
* @throws \Exception |
332
|
|
|
* @throws \Throwable |
333
|
|
|
*/ |
334
|
76 |
|
public function renderPhpFile($_file_, $_params_ = []) |
335
|
|
|
{ |
336
|
76 |
|
$_obInitialLevel_ = ob_get_level(); |
337
|
76 |
|
ob_start(); |
338
|
76 |
|
ob_implicit_flush(false); |
339
|
76 |
|
extract($_params_, EXTR_OVERWRITE); |
340
|
|
|
try { |
341
|
76 |
|
require $_file_; |
342
|
76 |
|
return ob_get_clean(); |
343
|
1 |
|
} catch (\Throwable $e) { |
344
|
1 |
|
while (ob_get_level() > $_obInitialLevel_) { |
345
|
1 |
|
if (!@ob_end_clean()) { |
346
|
|
|
ob_clean(); |
347
|
|
|
} |
348
|
|
|
} |
349
|
1 |
|
throw $e; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Renders dynamic content returned by the given PHP statements. |
355
|
|
|
* This method is mainly used together with content caching (fragment caching and page caching) |
356
|
|
|
* when some portions of the content (called *dynamic content*) should not be cached. |
357
|
|
|
* The dynamic content must be returned by some PHP statements. You can optionally pass |
358
|
|
|
* additional parameters that will be available as variables in the PHP statement: |
359
|
|
|
* |
360
|
|
|
* ```php |
361
|
|
|
* <?= $this->renderDynamic('return foo($myVar);', [ |
362
|
|
|
* 'myVar' => $model->getMyComplexVar(), |
363
|
|
|
* ]) ?> |
364
|
|
|
* ``` |
365
|
|
|
* @param string $statements the PHP statements for generating the dynamic content. |
366
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made |
367
|
|
|
* available in the $statement context. The parameters will be stored in the cache and be reused |
368
|
|
|
* each time $statement is executed. You should make sure, that these are safely serializable. |
369
|
|
|
* @throws \yii\base\ErrorException if the statement throws an exception in eval() |
370
|
|
|
* @return string the placeholder of the dynamic content, or the dynamic content if there is no |
371
|
|
|
* active content cache currently. |
372
|
|
|
*/ |
373
|
23 |
|
public function renderDynamic($statements, array $params = []) |
374
|
|
|
{ |
375
|
23 |
|
if (!empty($params)) { |
376
|
1 |
|
$statements = 'extract(unserialize(\'' . str_replace(['\\', '\'' ], ['\\\\', '\\\'' ], serialize($params)) . '\'));' . $statements; |
377
|
|
|
} |
378
|
|
|
|
379
|
23 |
|
if (!empty($this->_cacheStack)) { |
380
|
17 |
|
$n = count($this->_dynamicPlaceholders); |
381
|
17 |
|
$placeholder = "<![CDATA[YII-DYNAMIC-$n]]>"; |
382
|
17 |
|
$this->addDynamicPlaceholder($placeholder, $statements); |
383
|
|
|
|
384
|
17 |
|
return $placeholder; |
385
|
|
|
} |
386
|
|
|
|
387
|
6 |
|
return $this->evaluateDynamicContent($statements); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* {@inheritdoc} |
392
|
|
|
*/ |
393
|
1 |
|
public function getDynamicPlaceholders() |
394
|
|
|
{ |
395
|
1 |
|
return $this->_dynamicPlaceholders; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* {@inheritdoc} |
400
|
|
|
*/ |
401
|
|
|
public function setDynamicPlaceholders($placeholders) |
402
|
|
|
{ |
403
|
|
|
$this->_dynamicPlaceholders = $placeholders; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* {@inheritdoc} |
408
|
|
|
*/ |
409
|
17 |
|
public function addDynamicPlaceholder($placeholder, $statements) |
410
|
|
|
{ |
411
|
17 |
|
foreach ($this->_cacheStack as $cache) { |
412
|
17 |
|
$cache->addDynamicPlaceholder($placeholder, $statements); |
413
|
|
|
} |
414
|
|
|
|
415
|
17 |
|
$this->_dynamicPlaceholders[$placeholder] = $statements; |
416
|
17 |
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Evaluates the given PHP statements. |
420
|
|
|
* This method is mainly used internally to implement dynamic content feature. |
421
|
|
|
* @param string $statements the PHP statements to be evaluated. |
422
|
|
|
* @return mixed the return value of the PHP statements. |
423
|
|
|
*/ |
424
|
22 |
|
public function evaluateDynamicContent($statements) |
425
|
|
|
{ |
426
|
22 |
|
return eval($statements); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Returns a list of currently active dynamic content class instances. |
431
|
|
|
* @return DynamicContentAwareInterface[] class instances supporting dynamic contents. |
432
|
|
|
* @since 2.0.14 |
433
|
|
|
*/ |
434
|
16 |
|
public function getDynamicContents() |
435
|
|
|
{ |
436
|
16 |
|
return $this->_cacheStack; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Adds a class instance supporting dynamic contents to the end of a list of currently active |
441
|
|
|
* dynamic content class instances. |
442
|
|
|
* @param DynamicContentAwareInterface $instance class instance supporting dynamic contents. |
443
|
|
|
* @since 2.0.14 |
444
|
|
|
*/ |
445
|
22 |
|
public function pushDynamicContent(DynamicContentAwareInterface $instance) |
446
|
|
|
{ |
447
|
22 |
|
$this->_cacheStack[] = $instance; |
448
|
22 |
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Removes a last class instance supporting dynamic contents from a list of currently active |
452
|
|
|
* dynamic content class instances. |
453
|
|
|
* @since 2.0.14 |
454
|
|
|
*/ |
455
|
22 |
|
public function popDynamicContent() |
456
|
|
|
{ |
457
|
22 |
|
array_pop($this->_cacheStack); |
458
|
22 |
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Begins recording a block. |
462
|
|
|
* |
463
|
|
|
* This method is a shortcut to beginning [[Block]]. |
464
|
|
|
* @param string $id the block ID. |
465
|
|
|
* @param bool $renderInPlace whether to render the block content in place. |
466
|
|
|
* Defaults to false, meaning the captured block will not be displayed. |
467
|
|
|
* @return Block the Block widget instance |
468
|
|
|
*/ |
469
|
|
|
public function beginBlock($id, $renderInPlace = false) |
470
|
|
|
{ |
471
|
|
|
return Block::begin([ |
472
|
|
|
'id' => $id, |
473
|
|
|
'renderInPlace' => $renderInPlace, |
474
|
|
|
'view' => $this, |
475
|
|
|
]); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Ends recording a block. |
480
|
|
|
*/ |
481
|
|
|
public function endBlock() |
482
|
|
|
{ |
483
|
|
|
Block::end(); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Begins the rendering of content that is to be decorated by the specified view. |
488
|
|
|
* |
489
|
|
|
* This method can be used to implement nested layout. For example, a layout can be embedded |
490
|
|
|
* in another layout file specified as '@app/views/layouts/base.php' like the following: |
491
|
|
|
* |
492
|
|
|
* ```php |
493
|
|
|
* <?php $this->beginContent('@app/views/layouts/base.php'); ?> |
494
|
|
|
* //...layout content here... |
495
|
|
|
* <?php $this->endContent(); ?> |
496
|
|
|
* ``` |
497
|
|
|
* |
498
|
|
|
* @param string $viewFile the view file that will be used to decorate the content enclosed by this widget. |
499
|
|
|
* This can be specified as either the view file path or [path alias](guide:concept-aliases). |
500
|
|
|
* @param array $params the variables (name => value) to be extracted and made available in the decorative view. |
501
|
|
|
* @return ContentDecorator the ContentDecorator widget instance |
502
|
|
|
* @see ContentDecorator |
503
|
|
|
*/ |
504
|
|
|
public function beginContent($viewFile, $params = []) |
505
|
|
|
{ |
506
|
|
|
return ContentDecorator::begin([ |
507
|
|
|
'viewFile' => $viewFile, |
508
|
|
|
'params' => $params, |
509
|
|
|
'view' => $this, |
510
|
|
|
]); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Ends the rendering of content. |
515
|
|
|
*/ |
516
|
|
|
public function endContent() |
517
|
|
|
{ |
518
|
|
|
ContentDecorator::end(); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Begins fragment caching. |
523
|
|
|
* |
524
|
|
|
* This method will display cached content if it is available. |
525
|
|
|
* If not, it will start caching and would expect an [[endCache()]] |
526
|
|
|
* call to end the cache and save the content into cache. |
527
|
|
|
* A typical usage of fragment caching is as follows, |
528
|
|
|
* |
529
|
|
|
* ```php |
530
|
|
|
* if ($this->beginCache($id)) { |
531
|
|
|
* // ...generate content here |
532
|
|
|
* $this->endCache(); |
533
|
|
|
* } |
534
|
|
|
* ``` |
535
|
|
|
* |
536
|
|
|
* @param string $id a unique ID identifying the fragment to be cached. |
537
|
|
|
* @param array $properties initial property values for [[FragmentCache]] |
538
|
|
|
* @return bool whether you should generate the content for caching. |
539
|
|
|
* False if the cached version is available. |
540
|
|
|
*/ |
541
|
11 |
|
public function beginCache($id, $properties = []) |
542
|
|
|
{ |
543
|
11 |
|
$properties['id'] = $id; |
544
|
11 |
|
$properties['view'] = $this; |
545
|
|
|
/* @var $cache FragmentCache */ |
546
|
11 |
|
$cache = FragmentCache::begin($properties); |
547
|
11 |
|
if ($cache->getCachedContent() !== false) { |
548
|
7 |
|
$this->endCache(); |
549
|
|
|
|
550
|
7 |
|
return false; |
551
|
|
|
} |
552
|
|
|
|
553
|
11 |
|
return true; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Ends fragment caching. |
558
|
|
|
*/ |
559
|
11 |
|
public function endCache() |
560
|
|
|
{ |
561
|
11 |
|
FragmentCache::end(); |
562
|
11 |
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* Marks the beginning of a page. |
566
|
|
|
*/ |
567
|
50 |
|
public function beginPage() |
568
|
|
|
{ |
569
|
50 |
|
ob_start(); |
570
|
50 |
|
ob_implicit_flush(false); |
571
|
|
|
|
572
|
50 |
|
$this->trigger(self::EVENT_BEGIN_PAGE); |
573
|
50 |
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Marks the ending of a page. |
577
|
|
|
*/ |
578
|
|
|
public function endPage() |
579
|
|
|
{ |
580
|
|
|
$this->trigger(self::EVENT_END_PAGE); |
581
|
|
|
ob_end_flush(); |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.