Setting Multiple Layouts

Can the layout be set inside a view file?  I see it can be done within the controller, but didn't find anything about setting it within a view file.  In the documentation, it's stated that it can also be changed using CWebApplication:Layout.  I didn't see an method there to set the layout file. 



you can set CController::layout in the view file using:


Thanks Qiang. 

I already tried that and it doesn’t change the layout.  The new layout file is in the same directory as the main.php layout file so I can’t see it being a path issue.  I checked the path by echoing Yii::app()->layoutPath and it is protected/views/layouts.  I tried to assign it as follows in the view. 

<?php $this->layout=“newlayout”; ?>
.  Newlayout being newlayout.php. 

It works fine when I change it within the contoller class using $layout = "newlayout". 

Any ideas?


Amendment to previous reply…

I also echoed $this->layout after assigning the new layout and it does return the new layout file name, but the layout is not changing when refreshing.  The newlayout file definitely has a different layout which has been tested by changing it within the controller class. 


Ah, thank you for finding out this. I just fixed it. It is because layout was determined before the view is actually rendered. I just swapped the order. It should work now.

That's great Qiang.  How do I get a copy of the fix? 



you will need to use SVN or download from nightly snapshot (the fix will be in tomorrow's snapshot). Check the download page. The fix is currently in 1.0 branch.

Got it and it works great. 

Thanks for the quick response.


I have a follow up question.

If I were separating my actions out from the main controller:


class TestController extends CController {

	public function actions() {

		return array(

			'index' => 'application.controllers.test.IndexAction',

			'create' => 'application.controllers.test.CreateAction',




Currently I am changing the layout like this:


class CreateAction extends CAction {


	private $controller;


	public function __construct() {

		$this -> controller = new CController('Test');

		$this -> controller -> layout = "newlayout";



	public function run() {

		$this -> controller -> render('create', array(

			"layout" => $this -> controller -> layout,

			"time" => date('d/m/y H:i:s', time())));



Is there a way to globally change my layout for this controller instead of declaring the new layout in each action.

Sorry if this is documented, I'm still reading through.


Your action classes don't need to create the controller instance because it is already there. You can set the "layout" property in your TestController class:


class TestController extends CController


   public $layout="xyz";


How would my Action access the controller instance, as the action itself is not contained within the controller.

Thank you for your help, sorry if I am being a little newbie :)


Just use $this->controller because 'controller' is a property defined by the getController() method.

Ah I see, thank you. Works perfectly :)

this does not work under modules same examples any ideas about this?