The first argument to [[yii\base\Controller::render()|render()]] is the name of the view to display.
The above layout is used to generate HTML pages. It generates HTML tags that are common to all pages. You may
In the context of the controller, Yii will search for its views in `views/site/` where `site`
also generate other common HTML tags in the layout, such as head tags, main menu, etc.
is the controller ID. For details on how the view name is resolved, refer to the [[yii\base\Controller::render()]] method.
The second argument to [[yii\base\Controller::render()|render()]] is a data array of key-value pairs.
Within a layout, you have access to a special variable named `$content`. This is the only variable injected into
Through this array, data can be passed to the view, making the value available in the view as a variable
the layout by the controller when the [[yii\base\Controller::render()]] method is called to render a view.
named the same as the corresponding key.
The value of `$content` represents the rendering result of the view. As you can see in the above code,
`$content` is embedded within the body part of the layout.
The view for the action above would be `views/site/index.php` and can be something like:
Besides `$content`, you can also access the [[yii\base\View|view]] component via `$this`, like in normal views.
```php
<p>Hello, <?=$username?>!</p>
```
Any data type can be passed to the view, including arrays or objects.
### Organizing Layouts
Besides the above [[yii\web\Controller::render()|render()]] method, the [[yii\web\Controller]] class also provides
several other rendering methods. Below is a summary of these methods:
*[[yii\web\Controller::render()|render()]]: renders a view and applies the layout to the rendering result.
This is most commonly used to render a complete page.
*[[yii\web\Controller::renderPartial()|renderPartial()]]: renders a view without applying any layout.
This is often used to render a fragment of a page.
*[[yii\web\Controller::renderAjax()|renderAjax()]]: renders a view without applying any layout, and injects all
registered JS/CSS scripts and files. It is most commonly used to render an HTML output to respond to an AJAX request.
*[[yii\web\Controller::renderFile()|renderFile()]]: renders a view file. This is similar to
[[yii\web\Controller::renderPartial()|renderPartial()]] except that it takes the file path
of the view instead of the view name.
Widgets
### Using Layouts
-------
Widgets are self-contained building blocks for your views, a way to combine complex logic, display, and functionality into a single component. A widget:
A layout is applied when you call the [[yii\base\Controller::render()|render()]] method in a controller. The method
will first render the view being requested; it will then render the layout specified by the [[yii\base\Controller::layout]]
property of the controller and push the rendering result of the view into the layout as a variable `$content`.
* May contain advanced PHP programming
* Is typically configurable
* Is often provided data to be displayed
* Returns HTML to be shown within the context of the view
There are a good number of widgets bundled with Yii, such as [active form](form.md),
breadcrumbs, menu, and [wrappers around bootstrap component framework](bootstrap-widgets.md). Additionally there are
extensions that provide more widgets, such as the official widget for [jQueryUI](http://www.jqueryui.com) components.
In order to use a widget, your view file would do the following:
### View Events
```php
// Note that you have to "echo" the result to display it
echo\yii\widgets\Menu::widget(['items'=>$items]);
// Passing an array to initialize the object properties
The [[yii\base\View|view]] component provides several *placeholder* methods, such as `head()` and `beginBody()`,
$form=\yii\widgets\ActiveForm::begin([
which generate placeholders which will be replaced later by
Widgets are self-contained building blocks for your views, a way to combine complex logic, display, and functionality into a single component. A widget:
* May contain advanced PHP programming
* Is typically configurable
* Is often provided data to be displayed
* Returns HTML to be shown within the context of the view
There are a good number of widgets bundled with Yii, such as [active form](form.md),
breadcrumbs, menu, and [wrappers around bootstrap component framework](bootstrap-widgets.md). Additionally there are
extensions that provide more widgets, such as the official widget for [jQueryUI](http://www.jqueryui.com) components.
In order to use a widget, your view file would do the following:
```php
// Note that you have to "echo" the result to display it
echo\yii\widgets\Menu::widget(['items'=>$items]);
// Passing an array to initialize the object properties