|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\View; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
|
8
|
|
|
use Yiisoft\Html\Html; |
|
9
|
|
|
use Yiisoft\View\Event\BodyBegin; |
|
10
|
|
|
use Yiisoft\View\Event\BodyEnd; |
|
11
|
|
|
use Yiisoft\View\Event\PageEnd; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* View represents a view object in the MVC pattern. |
|
15
|
|
|
* |
|
16
|
|
|
* View provides a set of methods (e.g. {@see render()} for rendering purpose. |
|
17
|
|
|
* |
|
18
|
|
|
* You can modify its configuration by adding an array to your application config under `components` as it is shown in |
|
19
|
|
|
* the following example: |
|
20
|
|
|
* |
|
21
|
|
|
* ```php |
|
22
|
|
|
* 'view' => [ |
|
23
|
|
|
* 'theme' => 'app\themes\MyTheme', |
|
24
|
|
|
* 'renderers' => [ |
|
25
|
|
|
* // you may add Smarty or Twig renderer here |
|
26
|
|
|
* ] |
|
27
|
|
|
* // ... |
|
28
|
|
|
* ] |
|
29
|
|
|
* ``` |
|
30
|
|
|
* |
|
31
|
|
|
* For more details and usage information on View, see the [guide article on views](guide:structure-views). |
|
32
|
|
|
*/ |
|
33
|
|
|
class WebView extends View |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* The location of registered JavaScript code block or files. |
|
37
|
|
|
* This means the location is in the head section. |
|
38
|
|
|
*/ |
|
39
|
|
|
public const POSITION_HEAD = 1; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The location of registered JavaScript code block or files. |
|
43
|
|
|
* This means the location is at the beginning of the body section. |
|
44
|
|
|
*/ |
|
45
|
|
|
public const POSITION_BEGIN = 2; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The location of registered JavaScript code block or files. |
|
49
|
|
|
* This means the location is at the end of the body section. |
|
50
|
|
|
*/ |
|
51
|
|
|
public const POSITION_END = 3; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The location of registered JavaScript code block. |
|
55
|
|
|
* This means the JavaScript code block will be executed when HTML document composition is ready. |
|
56
|
|
|
*/ |
|
57
|
|
|
public const POSITION_READY = 4; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The location of registered JavaScript code block. |
|
61
|
|
|
* This means the JavaScript code block will be executed when HTML page is completely loaded. |
|
62
|
|
|
*/ |
|
63
|
|
|
public const POSITION_LOAD = 5; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* This is internally used as the placeholder for receiving the content registered for the head section. |
|
67
|
|
|
*/ |
|
68
|
|
|
private const PLACEHOLDER_HEAD = '<![CDATA[YII-BLOCK-HEAD-%s]]>'; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* This is internally used as the placeholder for receiving the content registered for the beginning of the body |
|
72
|
|
|
* section. |
|
73
|
|
|
*/ |
|
74
|
|
|
private const PLACEHOLDER_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN-%s]]>'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* This is internally used as the placeholder for receiving the content registered for the end of the body section. |
|
78
|
|
|
*/ |
|
79
|
|
|
private const PLACEHOLDER_BODY_END = '<![CDATA[YII-BLOCK-BODY-END-%s]]>'; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var string the page title |
|
83
|
|
|
*/ |
|
84
|
|
|
private string $title = ''; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var array the registered meta tags. |
|
88
|
|
|
* |
|
89
|
|
|
* {@see registerMetaTag()} |
|
90
|
|
|
*/ |
|
91
|
|
|
private array $metaTags = []; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var array the registered link tags. |
|
95
|
|
|
* |
|
96
|
|
|
* {@see registerLinkTag()} |
|
97
|
|
|
*/ |
|
98
|
|
|
private array $linkTags = []; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @var array the registered CSS code blocks. |
|
102
|
|
|
* |
|
103
|
|
|
* {@see registerCss()} |
|
104
|
|
|
*/ |
|
105
|
|
|
private array $css = []; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var array the registered CSS files. |
|
109
|
|
|
* |
|
110
|
|
|
* {@see registerCssFile()} |
|
111
|
|
|
*/ |
|
112
|
|
|
private array $cssFiles = []; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @var array the registered JS code blocks |
|
116
|
|
|
* |
|
117
|
|
|
* {@see registerJs()} |
|
118
|
|
|
*/ |
|
119
|
|
|
private array $js = []; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @var array the registered JS files. |
|
123
|
|
|
* |
|
124
|
|
|
* {@see registerJsFile()} |
|
125
|
|
|
*/ |
|
126
|
|
|
private array $jsFiles = []; |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Marks the position of an HTML head section. |
|
130
|
|
|
*/ |
|
131
|
4 |
|
public function head(): void |
|
132
|
|
|
{ |
|
133
|
4 |
|
echo sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature()); |
|
134
|
4 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Marks the beginning of an HTML body section. |
|
138
|
|
|
*/ |
|
139
|
4 |
|
public function beginBody(): void |
|
140
|
|
|
{ |
|
141
|
4 |
|
echo sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature()); |
|
142
|
4 |
|
$this->eventDispatcher->dispatch(new BodyBegin($this->getViewFile())); |
|
|
|
|
|
|
143
|
4 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Marks the ending of an HTML body section. |
|
147
|
|
|
*/ |
|
148
|
4 |
|
public function endBody(): void |
|
149
|
|
|
{ |
|
150
|
4 |
|
$this->eventDispatcher->dispatch(new BodyEnd($this->getViewFile())); |
|
|
|
|
|
|
151
|
4 |
|
echo sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature()); |
|
152
|
4 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Marks the ending of an HTML page. |
|
156
|
|
|
* |
|
157
|
|
|
* @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at |
|
158
|
|
|
* {@see POSITION_READY} and {@see POSITION_LOAD} positions will be rendered at the end of the view like |
|
159
|
|
|
* normal scripts. |
|
160
|
|
|
*/ |
|
161
|
4 |
|
public function endPage($ajaxMode = false): void |
|
162
|
|
|
{ |
|
163
|
4 |
|
$this->eventDispatcher->dispatch(new PageEnd($this->getViewFile())); |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
4 |
|
$content = ob_get_clean(); |
|
166
|
|
|
|
|
167
|
4 |
|
echo strtr($content, [ |
|
168
|
4 |
|
sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature()) => $this->renderHeadHtml(), |
|
169
|
4 |
|
sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature()) => $this->renderBodyBeginHtml(), |
|
170
|
4 |
|
sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature()) => $this->renderBodyEndHtml($ajaxMode), |
|
171
|
|
|
]); |
|
172
|
|
|
|
|
173
|
4 |
|
$this->clear(); |
|
174
|
4 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Renders a view in response to an AJAX request. |
|
178
|
|
|
* |
|
179
|
|
|
* This method is similar to {@see render()} except that it will surround the view being rendered with the calls of |
|
180
|
|
|
* {@see beginPage()}, {@see head()}, {@see beginBody()}, {@see endBody()} and {@see endPage()}. By doing so, the |
|
181
|
|
|
* method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the |
|
182
|
|
|
* view. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $view the view name. Please refer to {@see render()} on how to specify this parameter. |
|
185
|
|
|
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view |
|
186
|
|
|
* file. |
|
187
|
|
|
* @param ViewContextInterface|null $context the context that the view should use for rendering the view. If null, |
|
188
|
|
|
* existing {@see context} will be used. |
|
189
|
|
|
* |
|
190
|
|
|
* @return string the rendering result |
|
191
|
|
|
* |
|
192
|
|
|
* {@see render()} |
|
193
|
|
|
*/ |
|
194
|
|
|
public function renderAjax(string $view, array $params = [], ?ViewContextInterface $context = null): string |
|
195
|
|
|
{ |
|
196
|
|
|
$viewFile = $this->findTemplateFile($view, $context); |
|
197
|
|
|
|
|
198
|
|
|
ob_start(); |
|
199
|
|
|
PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0); |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
$this->beginPage(); |
|
202
|
|
|
$this->head(); |
|
203
|
|
|
$this->beginBody(); |
|
204
|
|
|
echo $this->renderFile($viewFile, $params, $context); |
|
205
|
|
|
$this->endBody(); |
|
206
|
|
|
$this->endPage(true); |
|
207
|
|
|
|
|
208
|
|
|
return ob_get_clean(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Clears up the registered meta tags, link tags, css/js scripts and files. |
|
213
|
|
|
*/ |
|
214
|
4 |
|
public function clear(): void |
|
215
|
|
|
{ |
|
216
|
4 |
|
$this->metaTags = []; |
|
217
|
4 |
|
$this->linkTags = []; |
|
218
|
4 |
|
$this->css = []; |
|
219
|
4 |
|
$this->cssFiles = []; |
|
220
|
4 |
|
$this->js = []; |
|
221
|
4 |
|
$this->jsFiles = []; |
|
222
|
4 |
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Registers a meta tag. |
|
226
|
|
|
* |
|
227
|
|
|
* For example, a description meta tag can be added like the following: |
|
228
|
|
|
* |
|
229
|
|
|
* ```php |
|
230
|
|
|
* $view->registerMetaTag([ |
|
231
|
|
|
* 'name' => 'description', |
|
232
|
|
|
* 'content' => 'This website is about funny raccoons.' |
|
233
|
|
|
* ]); |
|
234
|
|
|
* ``` |
|
235
|
|
|
* |
|
236
|
|
|
* will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`. |
|
237
|
|
|
* |
|
238
|
|
|
* @param array $options the HTML attributes for the meta tag. |
|
239
|
|
|
* @param string $key the key that identifies the meta tag. If two meta tags are registered with the same key, the |
|
240
|
|
|
* latter will overwrite the former. If this is null, the new meta tag will be appended to the |
|
241
|
|
|
* existing ones. |
|
242
|
|
|
*/ |
|
243
|
|
|
public function registerMetaTag(array $options, string $key = null): void |
|
244
|
|
|
{ |
|
245
|
|
|
if ($key === null) { |
|
246
|
|
|
$this->metaTags[] = Html::tag('meta', '', $options); |
|
247
|
|
|
} else { |
|
248
|
|
|
$this->metaTags[$key] = Html::tag('meta', '', $options); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Registers a link tag. |
|
254
|
|
|
* |
|
255
|
|
|
* For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) can be added like the |
|
256
|
|
|
* following: |
|
257
|
|
|
* |
|
258
|
|
|
* ```php |
|
259
|
|
|
* $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']); |
|
260
|
|
|
* ``` |
|
261
|
|
|
* |
|
262
|
|
|
* which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`. |
|
263
|
|
|
* |
|
264
|
|
|
* **Note:** To register link tags for CSS stylesheets, use {@see registerCssFile()]} instead, which has more |
|
265
|
|
|
* options for this kind of link tag. |
|
266
|
|
|
* |
|
267
|
|
|
* @param array $options the HTML attributes for the link tag. |
|
268
|
|
|
* @param string|null $key the key that identifies the link tag. If two link tags are registered with the same |
|
269
|
|
|
* key, the latter will overwrite the former. If this is null, the new link tag will be appended |
|
270
|
|
|
* to the existing ones. |
|
271
|
|
|
*/ |
|
272
|
|
|
public function registerLinkTag(array $options, ?string $key = null): void |
|
273
|
|
|
{ |
|
274
|
|
|
if ($key === null) { |
|
275
|
|
|
$this->linkTags[] = Html::tag('link', '', $options); |
|
276
|
|
|
} else { |
|
277
|
|
|
$this->linkTags[$key] = Html::tag('link', '', $options); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Registers CSRF meta tags. |
|
283
|
|
|
* |
|
284
|
|
|
* They are rendered dynamically to retrieve a new CSRF token for each request. |
|
285
|
|
|
* |
|
286
|
|
|
* ```php |
|
287
|
|
|
* $view->registerCsrfMetaTags(); |
|
288
|
|
|
* ``` |
|
289
|
|
|
* |
|
290
|
|
|
* The above code will result in `<meta name="csrf-param" content="[\Yiisoft\Web\Request::$csrfParam]">` and |
|
291
|
|
|
* `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page. |
|
292
|
|
|
* |
|
293
|
|
|
* Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()` |
|
294
|
|
|
* from `yii.js`. |
|
295
|
|
|
*/ |
|
296
|
|
|
public function registerCsrfMetaTags(): void |
|
297
|
|
|
{ |
|
298
|
|
|
$this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return Yiisoft\Html\Html::csrfMetaTags();'); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Registers a CSS code block. |
|
303
|
|
|
* |
|
304
|
|
|
* @param string $css the content of the CSS code block to be registered |
|
305
|
|
|
* @param array $options the HTML attributes for the `<style>`-tag. |
|
306
|
|
|
* @param string $key the key that identifies the CSS code block. If null, it will use $css as the key. If two CSS |
|
307
|
|
|
* code blocks are registered with the same key, the latter will overwrite the former. |
|
308
|
|
|
*/ |
|
309
|
|
|
public function registerCss(string $css, array $options = [], string $key = null): void |
|
310
|
|
|
{ |
|
311
|
|
|
$key = $key ?: md5($css); |
|
312
|
|
|
$this->css[$key] = Html::style($css, $options); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Registers a CSS file. |
|
317
|
|
|
* |
|
318
|
|
|
* This method should be used for simple registration of CSS files. If you want to use features of |
|
319
|
|
|
* {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use |
|
320
|
|
|
* {@see \Yiisoft\Assets\AssetBundle}. |
|
321
|
|
|
* |
|
322
|
|
|
* @param string $url the CSS file to be registered. |
|
323
|
|
|
* @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()} |
|
324
|
|
|
* for the supported options. |
|
325
|
|
|
* @param string $key the key that identifies the CSS script file. If null, it will use $url as the key. If two CSS |
|
326
|
|
|
* files are registered with the same key, the latter will overwrite the former. |
|
327
|
|
|
*/ |
|
328
|
1 |
|
public function registerCssFile(string $url, array $options = [], string $key = null): void |
|
329
|
|
|
{ |
|
330
|
1 |
|
$key = $key ?: $url; |
|
331
|
|
|
|
|
332
|
1 |
|
$this->cssFiles[$key] = Html::cssFile($url, $options); |
|
333
|
1 |
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Registers a JS code block. |
|
337
|
|
|
* |
|
338
|
|
|
* @param string $js the JS code block to be registered |
|
339
|
|
|
* @param int $position the position at which the JS script tag should be inserted in a page. |
|
340
|
|
|
* |
|
341
|
|
|
* The possible values are: |
|
342
|
|
|
* |
|
343
|
|
|
* - {@see POSITION_HEAD}: in the head section |
|
344
|
|
|
* - {@see POSITION_BEGIN}: at the beginning of the body section |
|
345
|
|
|
* - {@see POSITION_END}: at the end of the body section. This is the default value. |
|
346
|
|
|
* - {@see POSITION_LOAD}: executed when HTML page is completely loaded. |
|
347
|
|
|
* - {@see POSITION_READY}: executed when HTML document composition is ready. |
|
348
|
|
|
* @param string $key the key that identifies the JS code block. If null, it will use $js as the key. If two JS code |
|
349
|
|
|
* blocks are registered with the same key, the latter will overwrite the former. |
|
350
|
|
|
*/ |
|
351
|
1 |
|
public function registerJs(string $js, int $position = self::POSITION_END, string $key = null): void |
|
352
|
|
|
{ |
|
353
|
1 |
|
$key = $key ?: md5($js); |
|
354
|
1 |
|
$this->js[$position][$key] = $js; |
|
355
|
1 |
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Registers a JS file. |
|
359
|
|
|
* |
|
360
|
|
|
* This method should be used for simple registration of JS files. If you want to use features of |
|
361
|
|
|
* {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use |
|
362
|
|
|
* {@see \Yiisoft\Assets\AssetBundle}. |
|
363
|
|
|
* |
|
364
|
|
|
* @param string $url the JS file to be registered. |
|
365
|
|
|
* @param array $options the HTML attributes for the script tag. The following options are specially handled and |
|
366
|
|
|
* are not treated as HTML attributes: |
|
367
|
|
|
* |
|
368
|
|
|
* - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
|
369
|
|
|
* * {@see POSITION_HEAD}: in the head section |
|
370
|
|
|
* * {@see POSITION_BEGIN}: at the beginning of the body section |
|
371
|
|
|
* * {@see POSITION_END}: at the end of the body section. This is the default value. |
|
372
|
|
|
* |
|
373
|
|
|
* Please refer to {@see \Yiisoft\Html\Html::jsFile()} for other supported options. |
|
374
|
|
|
* @param string $key the key that identifies the JS script file. If null, it will use $url as the key. If two JS |
|
375
|
|
|
* files are registered with the same key at the same position, the latter will overwrite the former. |
|
376
|
|
|
* Note that position option takes precedence, thus files registered with the same key, but different |
|
377
|
|
|
* position option will not override each other. |
|
378
|
|
|
*/ |
|
379
|
1 |
|
public function registerJsFile(string $url, array $options = [], string $key = null): void |
|
380
|
|
|
{ |
|
381
|
1 |
|
$key = $key ?: $url; |
|
382
|
|
|
|
|
383
|
1 |
|
$position = ArrayHelper::remove($options, 'position', self::POSITION_END); |
|
384
|
1 |
|
$this->jsFiles[$position][$key] = Html::jsFile($url, $options); |
|
385
|
1 |
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated |
|
389
|
|
|
* variable names. |
|
390
|
|
|
* |
|
391
|
|
|
* @param string $name Name of the variable |
|
392
|
|
|
* @param array|string $value Value of the variable |
|
393
|
|
|
* @param int $position the position in a page at which the JavaScript variable should be inserted. |
|
394
|
|
|
* |
|
395
|
|
|
* The possible values are: |
|
396
|
|
|
* |
|
397
|
|
|
* - {@see POSITION_HEAD}: in the head section. This is the default value. |
|
398
|
|
|
* - {@see POSITION_BEGIN}: at the beginning of the body section. |
|
399
|
|
|
* - {@see POSITION_END}: at the end of the body section. |
|
400
|
|
|
* - {@see POSITION_LOAD}: enclosed within jQuery(window).load(). |
|
401
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
|
402
|
|
|
* - {@see POSITION_READY}: enclosed within jQuery(document).ready(). |
|
403
|
|
|
* Note that by using this position, the method will automatically register the jQuery js file. |
|
404
|
|
|
*/ |
|
405
|
1 |
|
public function registerJsVar(string $name, $value, int $position = self::POSITION_HEAD): void |
|
406
|
|
|
{ |
|
407
|
1 |
|
$js = sprintf('var %s = %s;', $name, \Yiisoft\Json\Json::htmlEncode($value)); |
|
408
|
1 |
|
$this->registerJs($js, $position, $name); |
|
409
|
1 |
|
} |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* Renders the content to be inserted in the head section. |
|
413
|
|
|
* |
|
414
|
|
|
* The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
|
415
|
|
|
* |
|
416
|
|
|
* @return string the rendered content |
|
417
|
|
|
*/ |
|
418
|
4 |
|
protected function renderHeadHtml(): string |
|
419
|
|
|
{ |
|
420
|
4 |
|
$lines = []; |
|
421
|
4 |
|
if (!empty($this->metaTags)) { |
|
422
|
|
|
$lines[] = implode("\n", $this->metaTags); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
4 |
|
if (!empty($this->linkTags)) { |
|
426
|
|
|
$lines[] = implode("\n", $this->linkTags); |
|
427
|
|
|
} |
|
428
|
4 |
|
if (!empty($this->cssFiles)) { |
|
429
|
1 |
|
$lines[] = implode("\n", $this->cssFiles); |
|
430
|
|
|
} |
|
431
|
4 |
|
if (!empty($this->css)) { |
|
432
|
|
|
$lines[] = implode("\n", $this->css); |
|
433
|
|
|
} |
|
434
|
4 |
|
if (!empty($this->jsFiles[self::POSITION_HEAD])) { |
|
435
|
1 |
|
$lines[] = implode("\n", $this->jsFiles[self::POSITION_HEAD]); |
|
436
|
|
|
} |
|
437
|
4 |
|
if (!empty($this->js[self::POSITION_HEAD])) { |
|
438
|
1 |
|
$lines[] = Html::script(implode("\n", $this->js[self::POSITION_HEAD])); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
4 |
|
return empty($lines) ? '' : implode("\n", $lines); |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* Renders the content to be inserted at the beginning of the body section. |
|
446
|
|
|
* |
|
447
|
|
|
* The content is rendered using the registered JS code blocks and files. |
|
448
|
|
|
* |
|
449
|
|
|
* @return string the rendered content |
|
450
|
|
|
*/ |
|
451
|
4 |
|
protected function renderBodyBeginHtml(): string |
|
452
|
|
|
{ |
|
453
|
4 |
|
$lines = []; |
|
454
|
4 |
|
if (!empty($this->jsFiles[self::POSITION_BEGIN])) { |
|
455
|
1 |
|
$lines[] = implode("\n", $this->jsFiles[self::POSITION_BEGIN]); |
|
456
|
|
|
} |
|
457
|
4 |
|
if (!empty($this->js[self::POSITION_BEGIN])) { |
|
458
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POSITION_BEGIN])); |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
4 |
|
return empty($lines) ? '' : implode("\n", $lines); |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
/** |
|
465
|
|
|
* Renders the content to be inserted at the end of the body section. |
|
466
|
|
|
* |
|
467
|
|
|
* The content is rendered using the registered JS code blocks and files. |
|
468
|
|
|
* |
|
469
|
|
|
* @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at |
|
470
|
|
|
* {@see POSITION_READY} and {@see POSITION_LOAD} positions will be rendered at the end of the view like normal |
|
471
|
|
|
* scripts. |
|
472
|
|
|
* |
|
473
|
|
|
* @return string the rendered content |
|
474
|
|
|
*/ |
|
475
|
4 |
|
protected function renderBodyEndHtml(bool $ajaxMode): string |
|
476
|
|
|
{ |
|
477
|
4 |
|
$lines = []; |
|
478
|
|
|
|
|
479
|
4 |
|
if (!empty($this->jsFiles[self::POSITION_END])) { |
|
480
|
1 |
|
$lines[] = implode("\n", $this->jsFiles[self::POSITION_END]); |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
4 |
|
if ($ajaxMode) { |
|
484
|
|
|
$scripts = []; |
|
485
|
|
|
if (!empty($this->js[self::POSITION_END])) { |
|
486
|
|
|
$scripts[] = implode("\n", $this->js[self::POSITION_END]); |
|
487
|
|
|
} |
|
488
|
|
|
if (!empty($this->js[self::POSITION_READY])) { |
|
489
|
|
|
$scripts[] = implode("\n", $this->js[self::POSITION_READY]); |
|
490
|
|
|
} |
|
491
|
|
|
if (!empty($this->js[self::POSITION_LOAD])) { |
|
492
|
|
|
$scripts[] = implode("\n", $this->js[self::POSITION_LOAD]); |
|
493
|
|
|
} |
|
494
|
|
|
if (!empty($scripts)) { |
|
495
|
|
|
$lines[] = Html::script(implode("\n", $scripts)); |
|
496
|
|
|
} |
|
497
|
|
|
} else { |
|
498
|
4 |
|
if (!empty($this->js[self::POSITION_END])) { |
|
499
|
|
|
$lines[] = Html::script(implode("\n", $this->js[self::POSITION_END])); |
|
500
|
|
|
} |
|
501
|
4 |
|
if (!empty($this->js[self::POSITION_READY])) { |
|
502
|
|
|
$js = "document.addEventListener('DOMContentLoaded', function(event) {\n" . implode("\n", $this->js[self::POSITION_READY]) . "\n});"; |
|
503
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
|
504
|
|
|
} |
|
505
|
4 |
|
if (!empty($this->js[self::POSITION_LOAD])) { |
|
506
|
|
|
$js = "window.addEventListener('load', function (event) {\n" . implode("\n", $this->js[self::POSITION_LOAD]) . "\n});"; |
|
507
|
|
|
$lines[] = Html::script($js, ['type' => 'text/javascript']); |
|
508
|
|
|
} |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
4 |
|
return empty($lines) ? '' : implode("\n", $lines); |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Get title in views. |
|
516
|
|
|
* |
|
517
|
|
|
* in Layout: |
|
518
|
|
|
* |
|
519
|
|
|
* ```php |
|
520
|
|
|
* <title><?= Html::encode($this->getTitle()) ?></title> |
|
521
|
|
|
* ``` |
|
522
|
|
|
* |
|
523
|
|
|
* in Views: |
|
524
|
|
|
* |
|
525
|
|
|
* ```php |
|
526
|
|
|
* $this->setTitle('Web Application - Yii 3.0.'); |
|
527
|
|
|
* ``` |
|
528
|
|
|
* |
|
529
|
|
|
* @return string |
|
530
|
|
|
*/ |
|
531
|
|
|
public function getTitle(): string |
|
532
|
|
|
{ |
|
533
|
|
|
return $this->title; |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
/** |
|
537
|
|
|
* It processes the CSS configuration generated by the asset manager and converts it into HTML code. |
|
538
|
|
|
* |
|
539
|
|
|
* @param array $cssFiles |
|
540
|
|
|
*/ |
|
541
|
|
|
public function setCssFiles(array $cssFiles): void |
|
542
|
|
|
{ |
|
543
|
|
|
foreach ($cssFiles as $key => $value) { |
|
544
|
|
|
$this->registerCssFile( |
|
545
|
|
|
$cssFiles[$key]['url'], |
|
546
|
|
|
$cssFiles[$key]['attributes'] |
|
547
|
|
|
); |
|
548
|
|
|
} |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
/** |
|
552
|
|
|
* It processes the JS configuration generated by the asset manager and converts it into HTML code. |
|
553
|
|
|
* |
|
554
|
|
|
* @param array $jsFiles |
|
555
|
|
|
*/ |
|
556
|
|
|
public function setJsFiles(array $jsFiles): void |
|
557
|
|
|
{ |
|
558
|
|
|
foreach ($jsFiles as $key => $value) { |
|
559
|
|
|
$this->registerJsFile( |
|
560
|
|
|
$jsFiles[$key]['url'], |
|
561
|
|
|
$jsFiles[$key]['attributes'] |
|
562
|
|
|
); |
|
563
|
|
|
} |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* It processes the JS strings generated by the asset manager. |
|
568
|
|
|
* |
|
569
|
|
|
* @param array $jsStrings |
|
570
|
|
|
*/ |
|
571
|
|
|
public function setJsStrings(array $jsStrings): void |
|
572
|
|
|
{ |
|
573
|
|
|
foreach ($jsStrings as $value) { |
|
574
|
|
|
$this->registerJs( |
|
575
|
|
|
$value['string'], |
|
576
|
|
|
$value['attributes']['position'] |
|
577
|
|
|
); |
|
578
|
|
|
} |
|
579
|
|
|
} |
|
580
|
|
|
|
|
581
|
|
|
/** |
|
582
|
|
|
* It processes the JS variables generated by the asset manager and converts it into JS code. |
|
583
|
|
|
* |
|
584
|
|
|
* @param array $jsVar |
|
585
|
|
|
*/ |
|
586
|
|
|
public function setJsVar(array $jsVar): void |
|
587
|
|
|
{ |
|
588
|
|
|
foreach ($jsVar as $key => $value) { |
|
589
|
|
|
$this->registerJsVar( |
|
590
|
|
|
(string)$key, |
|
591
|
|
|
$value['variables'], |
|
592
|
|
|
$value['attributes']['position'] |
|
593
|
|
|
); |
|
594
|
|
|
} |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
/** |
|
598
|
|
|
* Set title in views. |
|
599
|
|
|
* |
|
600
|
|
|
* {@see getTitle()} |
|
601
|
|
|
* |
|
602
|
|
|
* @param string $value |
|
603
|
|
|
*/ |
|
604
|
|
|
public function setTitle($value): void |
|
605
|
|
|
{ |
|
606
|
|
|
$this->title = $value; |
|
607
|
|
|
} |
|
608
|
|
|
} |
|
609
|
|
|
|