Development - Views

General Info

A view is a (visual) representation of its model. It would ordinarily highlight certain attributes of the model and suppress others. In such way it's acting as a filter of presentation. A view is attached to its model (or model part) and gets the data necessary for the presentation from the model by asking questions. It may also update the model by sending appropriate messages and queries. Generally all view files are placed in protected/views/{controller-lowercase-name}/ directory. There is only one exclusion for view files, that related to script components and they must be placed in directory protected/views/components/.

Remeber, that view file name must be in lowercase! Example: views/posts/edit.php

Call View from the Action

The typical way to create a view in is to use method render of embedded view property of the Action.
Example of an action that calles view file:
class AccountController extends CController
    public function indexAction()
        // Call the view file

        // Or call another view file

Rendering Data - passing data to the View (template engine)

When generating the HTML code of the resulting web page, the view (template engine) processes the data received from the action. Data should be passed to the template engine using the following syntax:
/* Create new variable "title" */
$this->_view->title = 'some text here...';
/* Create new variable "text" */
$this->_view->text = 'other text here...';
/* Change value of variable "title" */
$title = 'changed title value';
$this->_view->title = $title;

/* Samples: 1-3 - call of view files from controller */

/* Sample 1 - full syntax */

/* Sample 2 - short syntax, missing controller means use current controller name */
/* partial rendering - (2nd parameter $isPartial = true) */
$this->_view->render('view', true);
/* partial rendering with returned content - for some specific methods (2nd parameter $isPartial = true, 3rd parameter $return = true) */
$result = $this->_view->render('view', true, true);

/* Sample 3 - call of another view */

/* Sample 4 - call of view from components */

/* Sample 5 - call of another view from current controller */
$data = array();
$data['user_id'] = 2;
$data['type'] = 'global';
$this->_view->renderView('statistics', $data);

/* Sample 6 - call of one view from another view */
$this->_view->renderView('tabs/emails', array('email'=>$email));
Now all variables that were created with $this->_view may be used in our view file with simple using of PHP echo command.
Here the example:
<?= $actionMessage; ?>

<div class="title">
    echo $title;

<div class="content">
    echo $text;
You may define page title and other meta tags as well as breadcrumbs:
    Website::setMetaTags(array('title'=>A::t('users', 'User Registration')));

    // Define breadcrumbs title
    $this->_breadcrumbsTitle = A::t('users', 'User Registration');

    // Define breadcrumbs for this page
    $this->_breadCrumbs = array(
        array('label'=>A::t('users', 'Home'), 'url'=>Website::getDefaultPage()),
        array('label'=>A::t('users', 'User Registration')),
<h1 class="title"><?= A::t('users', 'User Registration'); ?></h1>
<div class="block-body">
    <?= $actionMessage; ?>