Jump to:

23450 Posts in 18899 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Using a dropdown in CMS to change css file

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: 1041 Views
  • mschiefmaker
    Community Member
    187 Posts

    Using a dropdown in CMS to change css file Link to this post


    I have added a dropdown to my CMS to allow the administrator to choose the background colour of a page.

    class Page extends SiteTree {
       public static $db = array(
          'BgColour' => 'varchar'
       public static $has_one = array(
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.Content.Main", new DropdownField('Background', 'Background Colour',singleton('PageColourScheme')->dbObject('BgColour')->enumValues()
    ), 'Content');
       return $fields;

    class PageColourScheme extends DataObject {
        static $db = array(
          "BgColour" => "Enum('Pink, Purple, Green, Yellow','Pink')"

    This all seems to work fine but the script that detects the colour selected and then applies the right css file to the page

    <% if BgColour=Pink %>
             <% require themedCSS(pink_layout) %>
          <% else %>
             <% require themedCSS(green_layout) %>
          <% end_if %>
    fails in that both lines are returned in the template i.e. I get

    <link rel="stylesheet" type="text/css" href="http://www.lowdown.org.nz/themes/lowdown/css/pink_layout.css?m=1241341643" />
    <link rel="stylesheet" type="text/css" href="http://www.lowdown.org.nz/themes/lowdown/css/green_layout.css?m=1241344957" />

    I have a similar version of this script running so that the administrator can chose the header which is applied so I am guessing that this issue has something to do with it being in the <head> section of the script. Can I do this? If so where am I going wrong?



  • mschiefmaker
    Community Member
    187 Posts

    Re: Using a dropdown in CMS to change css file Link to this post

    Worked this out. Moved the if statement into the controller
    class Page_Controller extends ContentController {
       public function init() {
          if ($HeaderSetup == Pink)   {
           else {


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.