The first argument is the actual JS code we want to insert into the page. The second argument
determines where script should be inserted into the page. Possible values are:
-[[yii\web\View::POS_HEAD|View::POS_HEAD]] for head section.
-[[yii\web\View::POS_BEGIN|View::POS_BEGIN]] for right after opening `<body>`.
-[[yii\web\View::POS_END|View::POS_END]] for right before closing `</body>`.
-[[yii\web\View::POS_READY|View::POS_READY]] for executing code on document `ready` event. This will register [[yii\web\JqueryAsset|jQuery]] automatically.
-[[yii\web\View::POS_LOAD|View::POS_LOAD]] for executing code on document `load` event. This will register [[yii\web\JqueryAsset|jQuery]] automatically.
The last argument is a unique script ID that is used to identify code block and replace existing one with the same ID
instead of adding a new one. If you don't provide it, the JS code itself will be used as the ID.
An external script can be added like the following:
## Using View Components <a name="using-view-components"></a>
You may also wrap the view render result into a layout using [[yii\base\View::beginContent()]], [[yii\base\View::endContent()]].
This approach can be used while applying nested layouts:
[[yii\base\View|View components]] provides many view-related features. While you can get view components
by creating individual instances of [[yii\base\View]] or its child class, in most cases you will mainly use
the `view` application component. You can configure this component in [application configurations](structure-applications.md#application-configurations)
like the following:
```php
<?php$this->beginContent('//layouts/overall')?>
<divclass="content">
<?=$content?>
<div>
<?php$this->endContent()?>
[
// ...
'components'=>[
'view'=>[
'class'=>'app\components\View',
],
// ...
],
]
```
### Nested Layouts
### Accessing Data in Layouts
View components provide the following useful view-related features, each described in more details in a separate section:
## View Components
*[theming](output-theming.md): allows you to develop and change the theme for your Web site.
*[fragment caching](caching-fragment.md): allows you to cache a fragment within a Web page.
*[client script handling](output-client-scripts.md): supports CSS and JavaScript registration and rendering.
*[asset bundle handling](structure-assets.md): supports registering and rendering of [asset bundles](structure-assets.md).
*[alternative template engines](tutorial-template-engines.md): allows you to use other template engines, such as
<linktitle="Live News for Yii"rel="alternate"type="application/rss+xml"href="http://www.yiiframework.com/rss.xml/">
```
The first argument is the actual JS code we want to insert into the page. The second argument
determines where script should be inserted into the page. Possible values are:
Similar as [[yii\web\View::registerMetaTag()|registerMetaTags()]], you can specify a key when calling
[[yii\web\View::registerLinkTag()|registerLinkTag()]] to avoid generated repeated link tags.
-[[yii\web\View::POS_HEAD|View::POS_HEAD]] for head section.
-[[yii\web\View::POS_BEGIN|View::POS_BEGIN]] for right after opening `<body>`.
-[[yii\web\View::POS_END|View::POS_END]] for right before closing `</body>`.
-[[yii\web\View::POS_READY|View::POS_READY]] for executing code on document `ready` event. This will register [[yii\web\JqueryAsset|jQuery]] automatically.
-[[yii\web\View::POS_LOAD|View::POS_LOAD]] for executing code on document `load` event. This will register [[yii\web\JqueryAsset|jQuery]] automatically.
The last argument is a unique script ID that is used to identify code block and replace existing one with the same ID
instead of adding a new one. If you don't provide it, the JS code itself will be used as the ID.
## View Events <a name="view-events"></a>
An external script can be added like the following:
[[yii\base\View|View components]] trigger several events during the view rendering process. You may respond
to these events to inject content into views or process the rendering results before they are sent to end users.