In YII you cannot pass variables to a layout. This is by design.
Layout is a site-wide entity and therefore must not rely on any
particular controller. So, while your approach is working, it's
not clean in the way of architecture. I recommend you one of the
following:

How can I pass variable from static page file to layout. Like we can set $this->pageTitle in static page file, and then that variable is available in layout. My question is how can I pass another variable?

I recently ran into this problem myself - I've inadvertently created a dependency between my controllers and layout, which turns out to be highly impractical, because I have certain controllers I reuse between projects. By creating a dependency on certain properties or methods of your controller to complete your layout, you lose that flexibility.

Seeing the other ideas posted here, I'm thinking I should have used the clips feature. Since we're dealing with a layout, most likely you're just trying to have multiple areas you can populate with content, like for example a left, right and center column?

If so, in your view, you should be able to capture a piece of content for the left column, for example, by using $this->beginClip('left') and $this->endClip() ... the capture snippet of content is now stored in the $clips collection of your Controller, and should be available when your layout renders.

In your layout, you can then do something like: if ($this->clips->contains('left')) echo $this->clips['left'];

Because clips are available in any controller, this is a cleaner approach that does not create any dependecy on any particular controller or base-controller.

Conceivably, you could even use $clips->contains() to make you layout flexible - e.g. if there's no content for the left column, you might add a class to the <body> tag and CSS that makes your center column wider.

Also note, you don't need a clip for the center column - as the main content from your view (outside any clip area) will of course still be captured and passed as $content to your layout.

Hope this is useful... personally, I have some refactoring to do myself tomorrow morning... ;-)

I was hoping to use beginContent() to actually render the main template from my invidual layouts, but that approach is no good, because the content renders in the context of a widget, so at that point it's too late to use CClientScript to add your styles and scripts - since the widget renders the template, you also don't have direct access to clips captured by your controller's view.

Note how I'm using CController::$layout directly as the class-name for my <body> element.

As you can see, my views have dependencies on a number of widgets, obtained using for example Yii::app()->getMenu() ... this is implemented as a behavior, attached to the application - so in my application config:

As you can see, I also took my $pageTitle out of the controller, and placed it in the layout behavior - I can access this as Yii::app()->pageTitle ... that was the last and only controller-dependency my layout had, so now it's completely unshackled and can be rendered from any controller!