Path alias for theme layout?

In a module I can use : public $layout=‘application.views.layouts.main’;

But how can I access the theme layout? public $layout=‘application.theme.views.layouts.main’;

or smething like…

$this->setLayoutPath(Yii::app()->theme->basePath);

In the XxxModule.php does not work also :(

There is a typo: ‘themes’ and not ‘theme’ in dot notation.

But if I understand correctly your question :D you can try:




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



Let me know if it works or not

ciao

danilo

You can use this function:




$this->getLayoutFile('main')



It works fine, in a module retrive module/module_id/views/layout/main.php and, if theme/themeId/module/module_id/views/layout/main.php is defined, it retrive the second one.

A function cannot be used in


public $layout =...

that mean that you have to implement some beforeAction or smth like that for set.

How to do with namespace (and avoid to implement beforeAction) i don’t know, maybe is not possible.

CTheme is a nice stuff, very powerfull, but is a bit difficoult to do smth that is not "rendering a view". Even "rendering a layout view" is difficoult.

If you find a better solution, tell me. If not, my suggestion works

Thanks the webroot.themes.themename works only it would be nice if the themename could be a variable? :P any chance of this?

The before view does not work…

public function beforeAction($event)

{


    $this->layout = $this->getLayoutFile('main');


    return true;


}

Does not find the layout unless i put the layout in a theme/module/layouts dir.

Btw is there a overview somewhere with all path aliases? its kinda tricky to hunt down :P

This is strange…

The getLayoutFile search first under the active theme directory and if don’t find it search under the ordinary path.

The philosophy of theming is to change the view to render, so you need a view for each theme you want to implement. Usually I use a main.php view static in wich I render a small view only for css inclusion.

That avoid the problem of replicate the code that there is in main.php (eg mainmenu).

Overview on path alias and theming are in The definitive guide to Yii

For a variable theme name I think you have to put layout declaration in the init() function of parent controller in this way:




public function init(){

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

}



HTH

Thx but is there any reason this is not working in init function in BlogModule.php? CWebModule does seem to have $this->layout property?

I think you don’t have to put that code inside init() function of BlogModule if it extends CWebModule, as I suppose.

I’ve done in this way: I have a main controller inside ‘components’ dir, for example MyController.php. Inside init() function of that I’ve put code above. After that I declare that other controllers extends MyController.

This rule is valid also for modules’ controllers. So inside protected/modules/myModule/controller/ModuleController.php I have something like this:




class ModuleController extends MyController

{

// put your code here...

}



so it inherits layout setting from MyController (but of course you can override inside ModuleController). I think init() in CWebModule refers to something else.

Let me know :)

Ciao

Danilo

Thanks,

Worked Fine for me.

Personnally, i’ve chosen to not modify gii generated class that contains $layout=’//layouts/*’ path.

Instead, i use a beforeAction method in global component Controller.php :




	/**

	 * Manage theme layout is overriden. 

	 * If given layout is overriden, use it instead of protected folder one. Else, use protected folder one.

	 * @see CController::beforeAction()

	 */

	public function beforeAction($action)

	{

		// If application is using a theme, replace default layout controller variable that start with '//layouts/' with a theme link

		if(empty(Yii::app()->theme->name) == false && isset($this->layout) == true && strpos($this->layout, '//layouts/') === 0)

		{			

			// Replace path with slash by dot.

 			$sThemeLayout = 'webroot.themes.'.Yii::app()->theme->name.'.views.layouts.'.str_replace('/', '.', substr($this->layout,10));

 			

 			// If theme override given layout, get it from theme

 			if($this->getLayoutFile($sThemeLayout) !== false)

 				$this->layout = $sThemeLayout; 			

		}

		return true;

	}

	

Because I’m using Yii at version 1.0,

CTheme and CController methods for resolving View paths act different (I’m convinced) than nowadays.

My solution was to fix Module’s layout property to “//layouts/main”.

Firsts two slashes forced view file resolution to be against a basePath (nor ViewPath).

That basePath, when already set a theme, becomes webroot.themes.views.

Hope it helps.

Xavier

Thanks it works.