Passed
Pull Request — master (#91)
by Alexander
21:32 queued 06:31
created

WebView::beginBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View;
6
7
use RuntimeException;
8
use Yiisoft\Html\Html;
9
use Yiisoft\Arrays\ArrayHelper;
10
use Yiisoft\Json\Json;
11
use Yiisoft\View\Event\BodyBegin;
12
use Yiisoft\View\Event\BodyEnd;
13
use Yiisoft\View\Event\PageEnd;
14
15
/**
16
 * View represents a view object in the MVC pattern.
17
 *
18
 * View provides a set of methods (e.g. {@see render()} for rendering purpose.
19
 *
20
 * You can modify its configuration by adding an array to your application config under `components` as it is shown in
21
 * the following example:
22
 *
23
 * ```php
24
 * 'view' => [
25
 *     'theme' => 'app\themes\MyTheme',
26
 *     'renderers' => [
27
 *         // you may add Smarty or Twig renderer here
28
 *     ]
29
 *     // ...
30
 * ]
31
 * ```
32
 *
33
 * For more details and usage information on View, see the [guide article on views](guide:structure-views).
34
 */
35
class WebView extends View
36
{
37
    /**
38
     * The location of registered JavaScript code block or files.
39
     * This means the location is in the head section.
40
     */
41
    public const POSITION_HEAD = 1;
42
43
    /**
44
     * The location of registered JavaScript code block or files.
45
     * This means the location is at the beginning of the body section.
46
     */
47
    public const POSITION_BEGIN = 2;
48
49
    /**
50
     * The location of registered JavaScript code block or files.
51
     * This means the location is at the end of the body section.
52
     */
53
    public const POSITION_END = 3;
54
55
    /**
56
     * The location of registered JavaScript code block.
57
     * This means the JavaScript code block will be executed when HTML document composition is ready.
58
     */
59
    public const POSITION_READY = 4;
60
61
    /**
62
     * The location of registered JavaScript code block.
63
     * This means the JavaScript code block will be executed when HTML page is completely loaded.
64
     */
65
    public const POSITION_LOAD = 5;
66
67
    /**
68
     * This is internally used as the placeholder for receiving the content registered for the head section.
69
     */
70
    private const PLACEHOLDER_HEAD = '<![CDATA[YII-BLOCK-HEAD-%s]]>';
71
72
    /**
73
     * This is internally used as the placeholder for receiving the content registered for the beginning of the body
74
     * section.
75
     */
76
    private const PLACEHOLDER_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN-%s]]>';
77
78
    /**
79
     * This is internally used as the placeholder for receiving the content registered for the end of the body section.
80
     */
81
    private const PLACEHOLDER_BODY_END = '<![CDATA[YII-BLOCK-BODY-END-%s]]>';
82
83
    /**
84
     * @var string the page title
85
     */
86
    private string $title;
87
88
    /**
89
     * @var array the registered meta tags.
90
     *
91
     * {@see registerMetaTag()}
92
     */
93
    private array $metaTags = [];
94
95
    /**
96
     * @var array the registered link tags.
97
     *
98
     * {@see registerLinkTag()}
99
     */
100
    private array $linkTags = [];
101
102
    /**
103
     * @var array the registered CSS code blocks.
104
     *
105
     * {@see registerCss()}
106
     */
107
    private array $css = [];
108
109
    /**
110
     * @var array the registered CSS files.
111
     *
112
     * {@see registerCssFile()}
113
     */
114
    private array $cssFiles = [];
115
116
    /**
117
     * @var array the registered JS code blocks
118
     *
119
     * {@see registerJs()}
120
     */
121
    private array $js = [];
122
123
    /**
124
     * @var array the registered JS files.
125
     *
126
     * {@see registerJsFile()}
127
     */
128
    private array $jsFiles = [];
129
130
    /**
131 4
     * Marks the position of an HTML head section.
132
     */
133 4
    public function head(): void
134 4
    {
135
        echo \sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature());
136
    }
137
138
    /**
139 4
     * Marks the beginning of an HTML body section.
140
     */
141 4
    public function beginBody(): void
142 4
    {
143 4
        echo \sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature());
144
        $this->eventDispatcher->dispatch(new BodyBegin($this->getViewFile()));
0 ignored issues
show
Bug introduced by
It seems like $this->getViewFile() can also be of type boolean; however, parameter $file of Yiisoft\View\Event\BodyBegin::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
        $this->eventDispatcher->dispatch(new BodyBegin(/** @scrutinizer ignore-type */ $this->getViewFile()));
Loading history...
145
    }
146
147
    /**
148 4
     * Marks the ending of an HTML body section.
149
     */
150 4
    public function endBody(): void
151 4
    {
152 4
        $this->eventDispatcher->dispatch(new BodyEnd($this->getViewFile()));
0 ignored issues
show
Bug introduced by
It seems like $this->getViewFile() can also be of type boolean; however, parameter $file of Yiisoft\View\Event\BodyEnd::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
        $this->eventDispatcher->dispatch(new BodyEnd(/** @scrutinizer ignore-type */ $this->getViewFile()));
Loading history...
153
        echo \sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature());
154
    }
155
156
    /**
157
     * Marks the ending of an HTML page.
158
     *
159
     * @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at
160
     * {@see POSITION_READY} and {@see POSITION_LOAD} positions will be rendered at the end of the view like
161 4
     * normal scripts.
162
     */
163 4
    public function endPage($ajaxMode = false): void
164
    {
165 4
        if ($this->getDynamicContents()) {
166
            throw new RuntimeException('The endPage method cannot be cached.');
167 4
        }
168 4
169 4
        $this->eventDispatcher->dispatch(new PageEnd($this->getViewFile()));
0 ignored issues
show
Bug introduced by
It seems like $this->getViewFile() can also be of type boolean; however, parameter $file of Yiisoft\View\Event\PageEnd::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
        $this->eventDispatcher->dispatch(new PageEnd(/** @scrutinizer ignore-type */ $this->getViewFile()));
Loading history...
170 4
171
        $content = \ob_get_clean();
172
173 4
        echo \strtr($content, [
174 4
            sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature()) => $this->renderHeadHtml(),
175
            sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature()) => $this->renderBodyBeginHtml(),
176
            sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature()) => $this->renderBodyEndHtml($ajaxMode),
177
        ]);
178
179
        $this->clear();
180
    }
181
182
    /**
183
     * Renders a view in response to an AJAX request.
184
     *
185
     * This method is similar to {@see render()} except that it will surround the view being rendered with the calls of
186
     * {@see beginPage()}, {@see head()}, {@see beginBody()}, {@see endBody()} and {@see endPage()}. By doing so, the
187
     * method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the
188
     * view.
189
     *
190
     * @param string $view the view name. Please refer to {@see render()} on how to specify this parameter.
191
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view
192
     * file.
193
     * @param ViewContextInterface|null $context the context that the view should use for rendering the view. If null,
194
     * existing {@see context} will be used.
195
     *
196
     * @return string the rendering result
197
     *
198
     * {@see render()}
199
     */
200
    public function renderAjax(string $view, array $params = [], ?ViewContextInterface $context = null): string
201
    {
202
        $viewFile = $this->findTemplateFile($view, $context);
203
204
        \ob_start();
205
        \ob_implicit_flush(0);
206
207
        $this->beginPage();
208
        $this->head();
209
        $this->beginBody();
210
        echo $this->renderFile($viewFile, $params, $context);
211
        $this->endBody();
212
        $this->endPage(true);
213
214
        return \ob_get_clean();
215
    }
216 4
217
    /**
218 4
     * Clears up the registered meta tags, link tags, css/js scripts and files.
219 4
     *
220 4
     * @return void
221 4
     */
222 4
    public function clear(): void
223 4
    {
224 4
        $this->metaTags = [];
225
        $this->linkTags = [];
226
        $this->css = [];
227
        $this->cssFiles = [];
228
        $this->js = [];
229
        $this->jsFiles = [];
230
    }
231
232
    /**
233
     * Registers a meta tag.
234
     *
235
     * For example, a description meta tag can be added like the following:
236
     *
237
     * ```php
238
     * $view->registerMetaTag([
239
     *     'name' => 'description',
240
     *     'content' => 'This website is about funny raccoons.'
241
     * ]);
242
     * ```
243
     *
244
     * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`.
245
     *
246
     * @param array $options the HTML attributes for the meta tag.
247
     * @param string $key the key that identifies the meta tag. If two meta tags are registered with the same key, the
248
     * latter will overwrite the former. If this is null, the new meta tag will be appended to the
249
     * existing ones.
250
     *
251
     * @return void
252
     */
253
    public function registerMetaTag(array $options, string $key = null): void
254
    {
255
        if ($this->getDynamicContents()) {
256
            $this->renderDynamic('$this->registerMetaTag($options, $key);', ['options' => $options, 'key' => $key]);
257
            return;
258
        }
259
        if ($key === null) {
260
            $this->metaTags[] = Html::tag('meta', '', $options);
261
        } else {
262
            $this->metaTags[$key] = Html::tag('meta', '', $options);
263
        }
264
    }
265
266
    /**
267
     * Registers a link tag.
268
     *
269
     * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) can be added like the
270
     * following:
271
     *
272
     * ```php
273
     * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
274
     * ```
275
     *
276
     * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`.
277
     *
278
     * **Note:** To register link tags for CSS stylesheets, use {@see registerCssFile()]} instead, which has more
279
     * options for this kind of link tag.
280
     *
281
     * @param array $options the HTML attributes for the link tag.
282
     * @param string|null $key the key that identifies the link tag. If two link tags are registered with the same
283
     * key, the latter will overwrite the former. If this is null, the new link tag will be appended
284
     * to the existing ones.
285
     */
286
    public function registerLinkTag(array $options, string $key = null): void
287
    {
288
        if ($this->getDynamicContents()) {
289
            $this->renderDynamic('$this->registerLinkTag($options, $key);', ['options' => $options, 'key' => $key]);
290
            return;
291
        }
292
        if ($key === null) {
293
            $this->linkTags[] = Html::tag('link', '', $options);
294
        } else {
295
            $this->linkTags[$key] = Html::tag('link', '', $options);
296
        }
297
    }
298
299
    /**
300
     * Registers CSRF meta tags.
301
     *
302
     * They are rendered dynamically to retrieve a new CSRF token for each request.
303
     *
304
     * ```php
305
     * $view->registerCsrfMetaTags();
306
     * ```
307
     *
308
     * The above code will result in `<meta name="csrf-param" content="[\Yiisoft\Web\Request::$csrfParam]">` and
309
     * `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page.
310
     *
311
     * Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()`
312
     * from `yii.js`.
313
     */
314
    public function registerCsrfMetaTags(): void
315
    {
316
        $this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return Yiisoft\Html\Html::csrfMetaTags();');
317
    }
318
319
    /**
320
     * Registers a CSS code block.
321
     *
322
     * @param string $css the content of the CSS code block to be registered
323
     * @param array $options the HTML attributes for the `<style>`-tag.
324
     * @param string $key the key that identifies the CSS code block. If null, it will use $css as the key. If two CSS
325
     * code blocks are registered with the same key, the latter will overwrite the former.
326
     */
327
    public function registerCss(string $css, array $options = [], string $key = null): void
328
    {
329
        if ($this->getDynamicContents()) {
330
            $this->renderDynamic('$this->registerCss($css, $options, $key);', ['css' => $css, 'options' => $options, 'key' => $key]);
331
            return;
332
        }
333
        $key = $key ?: \md5($css);
334
        $this->css[$key] = Html::style($css, $options);
335 1
    }
336
337 1
    /**
338
     * Registers a CSS file.
339 1
     *
340 1
     * This method should be used for simple registration of CSS files. If you want to use features of
341
     * {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use
342
     * {@see \Yiisoft\Assets\AssetBundle}.
343
     *
344
     * @param string $url the CSS file to be registered.
345
     * @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()}
346
     * for the supported options.
347
     *
348
     * @param string $key the key that identifies the CSS script file. If null, it will use $url as the key. If two CSS
349
     * files are registered with the same key, the latter will overwrite the former.
350
     *
351
     * @return void
352
     */
353
    public function registerCssFile(string $url, array $options = [], string $key = null): void
354
    {
355
        if ($this->getDynamicContents()) {
356
            $this->renderDynamic('$this->registerCssFile($url, $options, $key);', ['url' => $url, 'options' => $options, 'key' => $key]);
357
            return;
358
        }
359
        $key = $key ?: $url;
360
        $this->cssFiles[$key] = Html::cssFile($url, $options);
361 1
    }
362
363 1
    /**
364 1
     * Registers a JS code block.
365 1
     *
366
     * @param string $js the JS code block to be registered
367
     * @param int $position the position at which the JS script tag should be inserted in a page.
368
     *
369
     * The possible values are:
370
     *
371
     * - {@see POSITION_HEAD}: in the head section
372
     * - {@see POSITION_BEGIN}: at the beginning of the body section
373
     * - {@see POSITION_END}: at the end of the body section. This is the default value.
374
     * - {@see POSITION_LOAD}: executed when HTML page is completely loaded.
375
     * - {@see POSITION_READY}: executed when HTML document composition is ready.
376
     *
377
     * @param string $key the key that identifies the JS code block. If null, it will use $js as the key. If two JS code
378
     * blocks are registered with the same key, the latter will overwrite the former.
379
     *
380
     * @return void
381
     */
382
    public function registerJs(string $js, int $position = self::POSITION_END, string $key = null): void
383
    {
384
        if ($this->getDynamicContents()) {
385
            $this->renderDynamic('$this->registerJs($js, $position, $key);', ['js' => $js, 'position' => $position, 'key' => $key]);
386
            return;
387
        }
388
        $key = $key ?: \md5($js);
389
        $this->js[$position][$key] = $js;
390
    }
391
392 1
    /**
393
     * Registers a JS file.
394 1
     *
395
     * This method should be used for simple registration of JS files. If you want to use features of
396 1
     * {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use
397 1
     * {@see \Yiisoft\Assets\AssetBundle}.
398 1
     *
399
     * @param string $url the JS file to be registered.
400
     * @param array $options the HTML attributes for the script tag. The following options are specially handled and
401
     * are not treated as HTML attributes:
402
     *
403
     * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are:
404
     *     * {@see POSITION_HEAD}: in the head section
405
     *     * {@see POSITION_BEGIN}: at the beginning of the body section
406
     *     * {@see POSITION_END}: at the end of the body section. This is the default value.
407
     *
408
     * Please refer to {@see \Yiisoft\Html\Html::jsFile()} for other supported options.
409
     *
410
     * @param string $key the key that identifies the JS script file. If null, it will use $url as the key. If two JS
411
     * files are registered with the same key at the same position, the latter will overwrite the former.
412
     * Note that position option takes precedence, thus files registered with the same key, but different
413
     * position option will not override each other.
414
     *
415
     * @return void
416
     */
417
    public function registerJsFile(string $url, array $options = [], string $key = null): void
418 1
    {
419
        if ($this->getDynamicContents()) {
420 1
            $this->renderDynamic('$this->registerJsFile($url, $options, $key);', ['url' => $url, 'options' => $options, 'key' => $key]);
421 1
            return;
422 1
        }
423
        $key = $key ?: $url;
424
        $position = ArrayHelper::remove($options, 'position', self::POSITION_END);
425
        $this->jsFiles[$position][$key] = Html::jsFile($url, $options);
426
    }
427
428
    /**
429
     * Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated
430
     * variable names.
431 4
     *
432
     * @param string $name Name of the variable
433 4
     * @param array|string $value Value of the variable
434 4
     * @param int $position the position in a page at which the JavaScript variable should be inserted.
435
     *
436
     * The possible values are:
437
     *
438 4
     * - {@see POSITION_HEAD}: in the head section. This is the default value.
439
     * - {@see POSITION_BEGIN}: at the beginning of the body section.
440
     * - {@see POSITION_END}: at the end of the body section.
441 4
     * - {@see POSITION_LOAD}: enclosed within jQuery(window).load().
442 1
     *   Note that by using this position, the method will automatically register the jQuery js file.
443
     * - {@see POSITION_READY}: enclosed within jQuery(document).ready().
444 4
     *   Note that by using this position, the method will automatically register the jQuery js file.
445
     */
446
    public function registerJsVar(string $name, $value, int $position = self::POSITION_HEAD): void
447 4
    {
448 1
        $js = \sprintf('var %s = %s;', $name, Json::htmlEncode($value));
449
        $this->registerJs($js, $position, $name);
450 4
    }
451 1
452
    /**
453
     * Renders the content to be inserted in the head section.
454 4
     *
455
     * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.
456
     *
457
     * @return string the rendered content
458
     */
459
    protected function renderHeadHtml(): string
460
    {
461
        $lines = [];
462
        if (!empty($this->metaTags)) {
463
            $lines[] = \implode("\n", $this->metaTags);
464 4
        }
465
466 4
        if (!empty($this->linkTags)) {
467 4
            $lines[] = \implode("\n", $this->linkTags);
468 1
        }
469
        if (!empty($this->cssFiles)) {
470 4
            $lines[] = \implode("\n", $this->cssFiles);
471
        }
472
        if (!empty($this->css)) {
473
            $lines[] = \implode("\n", $this->css);
474 4
        }
475
        if (!empty($this->jsFiles[self::POSITION_HEAD])) {
476
            $lines[] = \implode("\n", $this->jsFiles[self::POSITION_HEAD]);
477
        }
478
        if (!empty($this->js[self::POSITION_HEAD])) {
479
            $lines[] = Html::script(\implode("\n", $this->js[self::POSITION_HEAD]));
480
        }
481
482
        return empty($lines) ? '' : \implode("\n", $lines) . "\n";
483
    }
484
485
    /**
486
     * Renders the content to be inserted at the beginning of the body section.
487
     *
488 4
     * The content is rendered using the registered JS code blocks and files.
489
     *
490 4
     * @return string the rendered content
491
     */
492 4
    protected function renderBodyBeginHtml(): string
493 1
    {
494
        $lines = [];
495
        if (!empty($this->jsFiles[self::POSITION_BEGIN])) {
496 4
            $lines[] = \implode("\n", $this->jsFiles[self::POSITION_BEGIN]);
497
        }
498
        if (!empty($this->js[self::POSITION_BEGIN])) {
499
            $lines[] = Html::script(\implode("\n", $this->js[self::POSITION_BEGIN]));
500
        }
501
502
        return empty($lines) ? '' : \implode("\n", $lines) . "\n";
503
    }
504
505
    /**
506
     * Renders the content to be inserted at the end of the body section.
507
     *
508
     * The content is rendered using the registered JS code blocks and files.
509
     *
510
     * @param bool $ajaxMode whether the view is rendering in AJAX mode. If true, the JS scripts registered at
511 4
     * {@see POSITION_READY} and {@see POSITION_LOAD} positions will be rendered at the end of the view like normal
512
     * scripts.
513
     *
514 4
     * @return string the rendered content
515
     */
516
    protected function renderBodyEndHtml(bool $ajaxMode): string
517
    {
518 4
        $lines = [];
519
520
        if (!empty($this->jsFiles[self::POSITION_END])) {
521
            $lines[] = \implode("\n", $this->jsFiles[self::POSITION_END]);
522
        }
523
524 4
        if ($ajaxMode) {
525
            $scripts = [];
526
            if (!empty($this->js[self::POSITION_END])) {
527
                $scripts[] = \implode("\n", $this->js[self::POSITION_END]);
528
            }
529
            if (!empty($this->js[self::POSITION_READY])) {
530
                $scripts[] = \implode("\n", $this->js[self::POSITION_READY]);
531
            }
532
            if (!empty($this->js[self::POSITION_LOAD])) {
533
                $scripts[] = \implode("\n", $this->js[self::POSITION_LOAD]);
534
            }
535
            if (!empty($scripts)) {
536
                $lines[] = Html::script(\implode("\n", $scripts));
537
            }
538
        } else {
539
            if (!empty($this->js[self::POSITION_END])) {
540
                $lines[] = Html::script(\implode("\n", $this->js[self::POSITION_END]));
541
            }
542
            if (!empty($this->js[self::POSITION_READY])) {
543
                $js = "document.addEventListener('DOMContentLoaded', function(event) {\n" . \implode("\n", $this->js[self::POSITION_READY]) . "\n});";
544
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
545
            }
546
            if (!empty($this->js[self::POSITION_LOAD])) {
547
                $js = "window.addEventListener('load', function (event) {\n" . \implode("\n", $this->js[self::POSITION_LOAD]) . "\n});";
548
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
549
            }
550
        }
551
552
        return empty($lines) ? '' : \implode("\n", $lines) . "\n";
553
    }
554
555
    /**
556
     * Get title in views.
557
     *
558
     *
559
     * in Layout:
560
     *
561
     * ```php
562
     * <title><?= Html::encode($this->getTitle()) ?></title>
563
     * ```
564
     *
565
     * in Views:
566
     *
567
     * ```php
568
     * $this->setTitle('Web Application - Yii 3.0.');
569
     * ```
570
     *
571
     * @return string
572
     */
573
    public function getTitle(): ?string
574
    {
575
        return $this->title ?? null;
576
    }
577
578
    /**
579
     * It processes the CSS configuration generated by the asset manager and converts it into HTML code.
580
     *
581
     * @param array $cssFiles
582
     * @return void
583
     */
584
    public function setCssFiles(array $cssFiles): void
585
    {
586
        if ($this->getDynamicContents()) {
587
            echo $this->renderDynamic('$this->setCssFiles($cssFiles);', ['cssFiles' => $cssFiles]);
588
            return;
589
        }
590
        foreach ($cssFiles as $key => $value) {
591
            $this->registerCssFile(
592
                $cssFiles[$key]['url'],
593
                $cssFiles[$key]['attributes']
594
            );
595 1
        }
596
    }
597
598
    /**
599
     * It processes the JS configuration generated by the asset manager and converts it into HTML code.
600
     *
601
     * @param array $jsFiles
602
     * @return void
603
     */
604
    public function setJsFiles(array $jsFiles): void
605
    {
606
        if ($this->getDynamicContents()) {
607
            echo $this->renderDynamic('$this->setJsFiles($jsFiles);', ['jsFiles' => $jsFiles]);
608
            return;
609
        }
610
        foreach ($jsFiles as $key => $value) {
611
            $this->registerJsFile(
612
                $jsFiles[$key]['url'],
613
                $jsFiles[$key]['attributes']
614
            );
615
        }
616
    }
617
618
    /**
619
     * Set title in views.
620
     *
621
     * {@see getTitle()}
622
     *
623
     * @param string $value
624
     * @return void
625
     */
626
    public function setTitle(string $value): void
627
    {
628
        if ($this->getDynamicContents()) {
629
            echo $this->renderDynamic('$this->setTitle($value);', ['value' => $value]);
630
        }
631
        $this->title = $value;
632
    }
633
}
634