changing layout on controller action views

In my Site controller i have different actions

    public function actionIndex()


{


	$this->render('index');


}





    public function actionPage()


{


	$this->layout='column1';


	$this->render('pages/about');


}

where i am changing my layout depending on actionID.

Now i want to change my layout depending on actionID view ie. on my actionID page i have two views

1.about

2.samples

for both of the views i need different layout. And at same time if my view does not exists it should redirect to error page (or) error layout should be rendered.

Could some one help me on this.

Thanks in advance,

Pavan

Hi Pavan,

First you have to declare

public $layout;




public $layout='column2';



as your controller’s property, then in every action change its value according to the layout you want, just like in your given example.




public function actionPage()

{

$this->layout='column1';

$this->render('pages/about');

}



That’s just about it. Just make sure that the layout you are calling exists (under ‘views’ folder), otherwise it won’t work. You might want to read this.

The same stuff goes with the error action (I find it interesting that you want to be prepared for the views that does not exist though)

macinville, controllers already have this property defined CController.layout.

You can do it inside views (actually I always do such assignments in views):




$this->layout = 'layout';



If a view is not found then an exception will be generated. If it’s not a debug mode a user will get error page. But if a layout is not found, a page will be rendered without it.

Hi andy_s,

Just corrected my post. Apologies if I made it sound like a new property.

After editing, it still doesn’t sound so helpful, does it?

I really need some sleep.

Hi Andy,Macin

as Andy said i did the layout property in my view files, but when rendering on actionID view i have a problem.

ie. for my actionID ‘page’ because of i have 2 views

1.about

I should render $this->render(‘pages/about’);

2.samples

I should render $this->render(‘pages/samples’);

how can i compare views and render accordingly in the same actionID (or) having a single render how can i make the second parameter dynamic(next to pages/{$dynamic})

Maybe you should take a look at the CViewAction. You can find a ready example in the default application generated by yiic tool.