How to theme a module

Hi guys,

I’m trying to theme my admin module to look different from the front-end of my website.

I have read through quite a few posts on module theming + the guide here…

http://www.yiiframework.com/doc/guide/1.1/en/topics.theming

But i still can’t get my module theme to change! It’s just displaying exactly the same as the rest of the website.

My directory structure is as follows.


WebRoot/

    assets

    protected/

        components/

        controllers/

	modules/

		admin/

        models/

        views/

            layouts/

                main.php

            site/

                index.php

    themes/

        ybr/

            views/

                layouts/

                    main.php

            	admin/

                	layouts/

                    	    main.php

I have also got this setting in my config/main.php file…

‘theme’=>‘ybr’,

Can anyone shed some light upon the subject?

Cheers

Tom

hello,

does it change the theme for the entire site if you set the ‘theme’ attribute in the [font=“Courier New”]main.php[/font]?

because if not, then the problem is something else … what Yii version are you using?

–iM

The primary theme attribute in the config is going to update for the whole site. You can’t set it as a param on the module in the config, because CModule doesn’t have a theme property, but you can add it to the init section of the Module class by setting


Yii::app()->theme = 'themename'; 

which will set it just for that module while it’s in use.

Spot on Dana. Thanks very much!

Glad to help :)

Hi I’ve got another question.

I’ve got 2 modules: “Rights” and “User”

With Right I don’t have a problem but with users I need more “Detailed” theming.

I mean User module have "user management" and login page.

I want to apply diferent layout (the same theme) just for the login page dependent is User logged in or not

I have tryied with something like this in controller:




 	public function checkLayout ()

	{

 		if (Yii::app()->user->checkAccess('panel')) 

			 $layout = "//layouts/pcolumn1";

		else 

			 $layout = "//layouts/column2";

			return $layout;

	} 

	

	

 public $layout = checkLayout ; 



but function doesn’t return any value.

Just this works.


public $layout = "//layouts/column2" ;

Any ideas ?

You need to use ‘$this->layout’ instead of $layout in that function. :)

It works ! Thanks :)

This is not working for me?? I have ‘theme’=>‘new’, in config/main.php which is expected to be inherited by any modules - ie, set the theme for the site, however both modules admin and shop refer back to the default theme??? I tried just setting the recommended global:




	public function init()

	{

		$this->setImport(array(

			'shop.models.*',

			'shop.components.*',

		));

		Yii::app()->theme = 'new'; 

	} 

but the ‘new’ theme is not getting used in the module??

Where should I look from here??

Hi, iam new here. Are you using yii shop?

i just figure out in line(86) productscontroller.php

just delete this line or anything suits your needs.


public function beforeAction($action) {

		$this->layout = Shop::module()->layout;

		return parent::beforeAction($action);

	}

Or under shopmodule.php change the layout




        public $adminLayout ='//layouts/column2'; //your layout

        public $layout='//layouts/column2';



[quote=“blevy009, post:9, topic:35265”]

This is not working for me?? I have ‘theme’=>‘new’, in config/main.php which is expected to be inherited by any modules - ie, set the theme for the site, however both modules admin and shop refer back to the default theme??? …[/code]

I had the exact same problem. I was extending the Trackstar project from the Agile Yii book. using Yii 1.1.8

I want my module to inherit the theme and layout from




 webroot.themes.new.views.layouts.main.



Tried all of the suggestions above with no luck.

I got it to work by explicitly declaring the $layout property of each controller the module using dot notation like this:

[i]

This code works in modules/MyModule/SomeController[/i]





class SomeController extends Controller

{

...

   public $layout = 'webroot.themes.new.views.layouts.main';

...

}




I have tried to set $this->layoutPath and $this->layout in the module init() function, but it is being ignored.

the following code does not work.





            	$this->layoutPath = 		Yii::getPathOfAlias('webroot.themes.'.Yii::app()->theme->name . '.views.layouts');

            	$this->layout = 'main';

            	

Greetings!

Where can I see the function init()?

Thanks!

in your module class which is default generated by Gii/Giix .

Hi all,

I use double forward slashes when setting theme url like this:


$this->layout = '//admin/layouts/column1';

When I changed to use sigle slash and it worn’t work. That setting is in defaulController.php inside admin Module folder.

Could you tell me why we use double slashes, and why single won’t work?

Thank you!

thanks its working fine