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