A view is a PHP script consisting mainly of user interface elements. It can contain PHP statements, but it is recommended that these statements should not alter data models and should remain relatively simple. In the spirit of separating of logic and presentation, large chunks of logic should be placed in controllers or models rather than in views.

A view has a name which is used to identify the view script file when rendering. The name of a view is the same as the name of its view script. For example, the view name edit refers to a view script named edit.php. To render a view, call CController::render() with the name of the view. The method will look for the corresponding view file under the directory protected/views/ControllerID.



$this->render('edit', array(

In the above, the render() method will extract the second array parameter into variables. As a result, in the view script we can access the local variables $var1 and $var2.

1. Layout

Layout is a special view that is used to decorate views. It usually contains parts of a user interface that are common among several views. For example, a layout may contain a header and a footer, and embed the view in between, like this:

<?php echo $content; ?>

where $content stores the rendering result of the view.


2. Widget

A widget is an instance of CWidget or a child class of CWidget. It is a component that is mainly for presentational purposes. A widget is usually embedded in a view script to generate a complex, yet self-contained user interface. For example, a calendar widget can be used to render a complex calendar user interface. Widgets facilitate better reusability in user interface code.






Widgets can be configured to customize their behavior. This is done by setting their initial property values when calling CBaseController::beginWidget or CBaseController::widget. For example, when using a CMaskedTextField widget, we might like to specify the mask being used. We can do so by passing an array of initial property values as follows, where the array keys are property names and array values are the initial values of the corresponding widget properties:



class MyWidget extends CWidget
    public function init()
    public function run()

Like a controller, a widget can also have its own view. By default, widget view files are located under the views subdirectory of the directory containing the widget class file. These views can be rendered by calling CWidget::render(), similar to that in controller. The only difference is that no layout will be applied to a widget view. Also, $this in the view refers to the widget instance instead of the controller instance.

Tip: CWidgetFactory::widgets can be used to configure widgets on a site-wide basis, allowing much easier base configuration. You can find more details on the theming page

3. System View





Total 2 comments

#8950 report it
rix.rix. at 2012/07/09 10:18am
Remember a view file can only be .php

Just a reminder a view file must have the .php extension to be rendered.

Twice now i've spent time debugging why view.htm would not render. DOH!

#4980 report it
Francis.TM at 2011/09/03 06:08am
To capture contents between CController::beginWidget() and CController::endWidget()

In the view:

<?php $this->beginWidget('ext.MyExt'); ?>
    here is some content

To capture contents between these methods, just using following codes. In MyExt:

class MyExt extends CWidget
    public function init()
    public function run()
        $content = ob_get_clean();
        // the variable above is what you want to captured,
        // and it will not output unless echo this variable.

Leave a comment

Please to leave your comment.