Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SSViewer::set_theme() deprecation - I need it!!


Go to End


5 Posts   1948 Views

Avatar
tazzydemon

Community Member, 135 Posts

19 June 2014 at 12:23pm

Edited: 19/06/2014 12:32pm

Guys

I have introduced a page level template choice which uses SSViewer::set_theme() as the means to set the theme on a per page basis. This appears to work perfectly and have little or no overhead. I still pull the javascript and anything else I can from the root config theme to make caching better.

Now I discover SSViewer::set_theme() is deprecated for yml config settings. I cant do a config inst() change like this or it will be global. whats the workaround - or alternatively PLEASE KEEP the method.

UPDATE: I see that it is already using Config::inst()->update('SSViewer', 'theme', $theme) in that method. This does not seem good.

This is what I am doing with my own static $db Page variable "Theme" in my Page.php Page_Controller init method


      switch ($this->Theme){

            case "Business":
                SSViewer::set_theme('business');
                break;
            case "Consumer":
                SSViewer::set_theme('consumer');
                break;
            default:
                SSViewer::set_theme('normal');
        }

Im guessing now that this is BAD!! Or is it? It does work. I can flit between different pages and they render and cache perfectly.

Julian

Avatar
micmania1

Community Member, 9 Posts

21 June 2014 at 2:49am

SSViewer::set_theme() works in exactly the same way as the new config system.

What you're trying to do isn't available out of the box. Your best plan of attack is probably to overload the getViewer() method in the controller then manipulate the main/Layout part of the template.

public function getViewer($action = null) {
	$viewer = parent::getViewer($action);
	$viewerTemplates = $viewer->templates();

	// Set your new theme
	$original = Config::inst()->get("SSViewer", "theme");
	Config::inst()->update("SSViewer", "theme", $this->Theme);

	$pageTheme = parent::getViewer($action);
	$pageTheme->setTemplateFile("main", $viewerTemplates['main']);
	
	// Reset to original 
	Config::inst()->update("SSViewer", "theme", $original);
	return $pageTheme;
}

I haven't tested this so you may very well encounter issues, but hopefully it'll give you a good start.

Avatar
mhanisch

Community Member, 5 Posts

31 July 2014 at 5:13am

related to this:
if i have one theme per page type, that is static (i.e. i don't need to dynamically get it from a db field) - is there a way to put that in the yml?
like:
Forum:
theme: 'forum'

Avatar
tazzydemon

Community Member, 135 Posts

31 July 2014 at 9:01am

Edited: 31/07/2014 9:02am

That one is easy.

Just make a copy of the root template:

/themese/<templatename>/templates/Page.ss

and rename it MyPageTypeClassName.ss. Modify it as you wish. Job done. Of course that can point to a whole new load of css and images, as you require. Don't forget to ?flush=all to see the new template

I realise that does not quite answer as you asked but it fulfills the same requirement and it's dead easy.

Avatar
martimiz

Forum Moderator, 1391 Posts

1 August 2014 at 2:11am

Edited: 01/08/2014 2:12am

Instead of using set_theme('MyTheme') in your Page_Controller, you should be able to use the config system, like the deprecation notice says. In the same location:

Config::inst()->update('SSViewer', 'theme', 'MyTheme');