Undefined variable: model in layout


I’m getting this error while trying to display a model’s property in a layout… it’s not in the view itself, it’s in the layout.

Here’s my controller:

public function actionView($id) {

        $this->layout = '//layouts/appContent';


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

            'model' => $this->loadModel($id),



and here is the appContent.php code:


 <h2><?php echo $model->name; ?></h2>


And the error says:

Undefined variable: model

Is it because I can not access variables outside the view itself?

Thanks in advance!

In layout you have not the variable of the views, you can only use $this for have the controller.

If you need some variable in the layout, diclare as public in the controller. Take a look at the class Controller in protected/components, here it declares breadcrumbs and menu that are used in latout/main and layout/column2

CController already has a property pageTitle. You can find it useful in this situation by setting it’s value in a controller:

$this->pageTitle = $model->name;

and then echo it in a layout:

<h2><?php echo $this->pageTitle; ?></h2>

But as zaccaria said, you can always define an own property.

Thanks for you replies!

I think I’m going to use a CClipWidget to do this, because a also need to show more properties from the model…

Thanks again!