Using a module-specific layout file

I just created an admin module for my app and I’d like to use a specific layout file to render it in.

I changed the layoutPath property in the init() function of protected/modules/admin/AdminModule.php and created a file main.php in protected/modules/admin/views/layouts with my alternate layout. However, when I go to index.php?r=admin, the content is still rendered in the global app layout.

What more do I need to do to make this happen?

Hi, I am learning Yii also and I am setting up admin and frontend layouts.

I followed this cookbook article in particular

this comment was very valuable.

I setup a frontend and backend controller and all of my controllers are extensions of these controllers.

In my case I wanted to be able to choose from various layouts (admin chooses) so that the look of the frontend can be easily changed by the admin. I store the name of the layout in a database and retrieve it in the front end controller along with a css file (again chosen by the admin).


<?php


class FrontendController extends CController

{


public function init(){

        parent::init();

//        Get the layout

        $this->layout = sitekeys::model()->find('sitekey=:sitekey',array(':sitekey'=>'layout'))->value;

//        Get the CSS

        $style = sitekeys::model()->find('sitekey=:sitekey',array(':sitekey'=>'stylesheet'))->value;

        

		// this registers a bunch of css files, the render command adds them to the web doc

        $CssFiles = array('reset','text','960',$style);

        foreach($CssFiles as $Css){

               Yii::app()->clientScript->registerCssFile('/css/'.$Css.'.css', '');

        }

        

    }

}

I know this doesn’t exactly answer your question but I hope it helps.

doodle ;)

you do not need to change the layout path. if you just create the main.php layout file for the module then it will use it automatically.

Well, no. I tried that first, but it didn’t work.

I have yet to touch on modules but have you tried the viewPath attribute?

doodle

Ahh, i see, that used to work. Must have changed at some point, anyway, i just tested this method and it should work just fine. Specify your layout file when you configure your modules in protected/config/main.php like this


	'modules'=>array(

		'accounts'=>array(

			'layout'=>'main',

		),

	),

it will search for main.php layout file under protected/modules/accounts/views/layouts

Thank you, that fixed it.