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.

General Questions

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

Using a dropdown in CMS to change css file


2 Posts   1051 Views


10 May 2009 at 10:12pm Community Member, 187 Posts


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="" />
<link rel="stylesheet" type="text/css" href="" />

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?




11 May 2009 at 7:36am Community Member, 187 Posts

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