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


Reply


2 Posts   1056 Views

Avatar
mschiefmaker

Community Member, 187 Posts

10 May 2009 at 10:12pm

Hi

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?

Thanks

MM

Avatar
mschiefmaker

Community Member, 187 Posts

11 May 2009 at 7:36am

Worked this out. Moved the if statement into the controller
class Page_Controller extends ContentController {
   
   public function init() {
      parent::init();
      if ($HeaderSetup == Pink)   {
       Requirements::themedCSS('pink_layout');}
       else {
       Requirements::themedCSS('green_layout');}
      }
Cheers

MM