Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » SSViewer::set_theme() deprecation - I need it!!

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

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

Page: 1
Go to End
Author Topic: 143 Views
  • tazzydemon
    Avatar
    Community Member
    121 Posts

    SSViewer::set_theme() deprecation - I need it!! Link to this post

    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

  • micmania1
    Avatar
    Community Member
    7 Posts

    Re: SSViewer::set_theme() deprecation - I need it!! Link to this post

    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.

  • mhanisch
    Avatar
    Community Member
    4 Posts

    Re: SSViewer::set_theme() deprecation - I need it!! Link to this post

    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'

  • tazzydemon
    Avatar
    Community Member
    121 Posts

    Re: SSViewer::set_theme() deprecation - I need it!! Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1068 Posts

    Re: SSViewer::set_theme() deprecation - I need it!! Link to this post

    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');

    143 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.