Yii Framework Forum: Revert back to original theme/layout if view not found in other theme - Yii Framework Forum

Jump to content

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

Revert back to original theme/layout if view not found in other theme Rate Topic: -----

#1 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 228
  • Joined: 20-June 10

Posted 09 September 2012 - 10:05 PM

Hi,

I have some problem about using Yii theming. I put it in the StackOverflow, but I'll also attach the question here.

http://stackoverflow...-in-other-theme

Quote

down vote favorite


For my mobile version of the site I'm using the theming method. Instead of creating a new module to handle mobile browsers, I just change the theme so that the url can be mapped 1 to 1 from mobile site to desktop site.

To detect mobile site, I'm using the DetectMobile function. Here's how it works. I create an event handler for onBeginRequest event in CWebApplication

if (detectmobile()){
    Yii::app()->theme = 'mobile';
} else {
    Yii::app()->theme = null;
}

Even though this method is easier than making a specific module to handle mobile site, sometimes the programmer forget to create view for a specific route. As we know in that case the view handler will use the view file from the base views directory, but it also will use the layout file from the theme.

My question is how to revert back the original theme (or to be precise, the original layout) if a route can not find the view in the theme.


0

#2 User is offline   Pep 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 06-August 11

Posted 18 March 2013 - 02:56 PM

Hi,

Here is the way I handled this.

The problem: If a view is not available in a theme, the application will use the default view outside the theme but it will continue to use the layout in the theme because the layout was found in the theme.

One possible solution: If a view is not found in the theme tell the application to stop using the theme.

My controllers extend a base controller (MyBaseController) which itself extends CController. Putting this code here in MyBaseController allows me to write this code only once. With this code the application will detect if a view is available for the current theme and if a view is not available then it will stop using the theme.


	protected function beforeRender($view)
	{
		// if view is not found in the theme, do not use the theme's layout
		if(($theme = Yii::app()->getTheme()) !== null && ($theme->getViewFile($this,$view)) === false)
		{
			Yii::app()->theme = null;
		}
		
		return true;
	}

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