View ==== The view component is an important part of MVC. The view acts as the interface to the application, making it responsible for presenting data to end users, displaying forms, and so forth. Basics ------ By default, Yii uses PHP in view templates to generate content and elements. A web application view typically contains some combination of HTML, along with PHP `echo`, `foreach`, `if`, and other basic constructs. Using complex PHP code in views is considered to be bad practice. When complex logic and functionality is needed, such code should either be moved to a controller or a widget. The view is typically called from controller action using the `render()` method: ```php public function actionIndex() { return $this->render('index', ['username' => 'samdark']); } ``` The first argument to `render()` is the name of the view to display. In the context of the controller, Yii will search for its views in `views/site/` where `site` 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 `render()` is a data array of key-value pairs. Through this array, data can be passed to the view, making the value available in the view as a variable named the same as the corresponding key. The view for the action above would be `views/site/index.php` and can be something like: ```php
Hello, = $username ?>!
``` Any data type can be passed to the view, including arrays or objects. Widgets ------- 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, dmenu, 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 $form = \yii\widgets\ActiveForm::begin([ 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => ['inputOptions' => ['class' => 'input-xlarge']], ]); ... form inputs here ... \yii\widgets\ActiveForm::end(); ``` In the first example in the code above, the `widget` method is used to invoke a widget that just outputs content. In the second example, `begin` and `end` are used for a widget that wraps content between method calls with its own output. In case of the form this output is the `