Jump to:

23550 Posts in 19336 Topics by 2890 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: 245 Views
  • tazzydemon
    Community Member
    122 Posts

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


    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":
    case "Consumer":

    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.


  • micmania1
    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
    Community Member
    5 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?
    theme: 'forum'

  • tazzydemon
    Community Member
    122 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:


    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
    Forum Moderator
    1091 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');

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.