Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Accessing theme templates in the CMS? [SOLVED]

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

Page: 1
Go to End
Author Topic: 407 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Accessing theme templates in the CMS? [SOLVED] Link to this post

    I'm extending LeftAndMain to create a custom CMS area. This is loosely based on the Settings area of the CMS.

    After looking into how the Settings area is put together in code, I noticed this function that sets the theme to display tabs correctly:

    public function getResponseNegotiator() {
          $neg = parent::getResponseNegotiator();
          $controller = $this;
          $neg->setCallback('CurrentForm', function() use(&$controller) {
             return $controller->renderWith($controller->getTemplatesWithSuffix('_Content'));
          });
          return $neg;
       }

    This gets a template located in /cms/templates/Includes/CMSSettingsController_Content.ss

    So I duplicated this in my custom LeftAndMain child and created a duplicate template. This is not a module as it is very specific to the application and we want to keep code/templates in one area of the file system.

    When I do the same as above, getTemplatesWithSuffix only returns LeftAndMain.ss.

    I have stepped through the code and found that even though my custom template is found, it is ignored because $theme is null in this subsequent function:
    SSViewer.php

    public static function hasTemplate($templates) {
          $manifest = SS_TemplateLoader::instance()->getManifest();

          if(Config::inst()->get('SSViewer', 'theme_enabled')) {
             $theme = Config::inst()->get('SSViewer', 'theme');
          } else {
             $theme = null;
          }

          foreach ((array) $templates as $template) {
             if ($manifest->getCandidateTemplate($template, $theme)) return true;
          }

          return false;
       }

    So how do I make the CMS aware of the theme? Or is a way of doing this?

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Accessing theme templates in the CMS? [SOLVED] Link to this post

    Worth adding: Theme is set in config.yml like so:

    ---
    Name: mysite
    After: 'framework/*','cms/*'
    ---
    # YAML configuration for SilverStripe
    # See http://doc.silverstripe.org/framework/en/topics/configuration
    # Caution: Indentation through two spaces, not tabs
    SSViewer:
    theme: 'mytheme'

    I thought the After: might have been the culprit, but even if I remove it entirely and do a dev/build, the CMS still can't get hold of it.

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Accessing theme templates in the CMS? [SOLVED] Link to this post

    The CMS deliberately unsets the theme, so it won't load templates from there. Put the templates in mysite/templates/ instead.

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Accessing theme templates in the CMS? [SOLVED] Link to this post

    Thanks Simon,

    I wasn't really interested in separating this one template from the rest of the codebase (for future maintenance reasons). But your suggestion did work.

    In then end I was able to get the template to load from the theme directory by adding this to my LeftAndMain extended class:

    public function init() {
       parent::init();
       
       Config::inst()->update('SSViewer', 'theme_enabled', true);
          
       ...
    }

    407 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.