Yii Framework Forum: Path alias for theme layout? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Path alias for theme layout? Rate Topic: **--- 1 Votes

#1 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 02 June 2010 - 10:40 AM

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 :(
1

#2 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 02 June 2010 - 11:19 AM

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
3

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 03 June 2010 - 05:47 AM

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
0

#4 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 03 June 2010 - 09:35 AM

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
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 03 June 2010 - 12:38 PM

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
0

#6 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 03 June 2010 - 03:12 PM

View Postmech7, on 03 June 2010 - 09:35 AM, said:

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


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
2

#7 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 04 June 2010 - 10:26 PM

View Postzitter, on 03 June 2010 - 03:12 PM, said:

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?
0

#8 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 05 June 2010 - 01:24 AM

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
0

#9 User is offline   Bijay Rungta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 27-August 10

Posted 16 December 2010 - 04:56 AM

View Postzitter, on 02 June 2010 - 11:19 AM, said:

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


Thanks,

Worked Fine for me.
0

#10 User is offline   Revelis Luc Bonnin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 23-May 11
  • Location:Earth

Posted 10 August 2011 - 08:00 AM

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;
	}
	

0

#11 User is offline   Xavier Subira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 12-November 12

Posted 28 November 2012 - 10:52 AM

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
0

#12 User is offline   hemc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 09-July 12
  • Location:New Delhi

Posted 26 July 2013 - 04:23 AM

View Postzitter, on 03 June 2010 - 03:12 PM, said:

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


Thanks it works.
Please don't add a reply to say thanks, There is already a +1 button to do same.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users