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:
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:
As shown above, the child layout content should be enclosed within [[yii\base\View::beginContent()|beginContent()]],
[[yii\base\View::endContent()|endContent()]]. The parameter passed to [[yii\base\View::beginContent()|beginContent()]]
specifies what is the parent layout. It can be either a layout file or alias.
```php
<?php$this->beginContent('//layouts/overall')?>
<divclass="content">
<?=$content?>
<div>
<?php$this->endContent()?>
```
Using the above approach, you can nest layouts in more than one levels.
### Nested Layouts
### Accessing Data in Layouts
## Using View Components <a name="using-view-components"></a>
[[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:
## View Components
```php
[
// ...
'components'=>[
'view'=>[
'class'=>'app\components\View',
],
// ...
],
]
```
### Setting page title
### Adding meta tags
### Registering link tags
### Registering CSS
### Registering scripts
### Static Pages
### Assets
### Alternative Template Engines
View components provide the following useful view-related features, each described in more details in a separate section:
*[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
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: