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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Adding variable .css files

Go to End

3 Posts   2243 Views


Community Member, 22 Posts

24 February 2009 at 12:14pm


I am putting together a site with 3 different sections, ie. Sales & Marketing, Branding & Default. Each section needs the same layout, but different colors in the submenu and headings.

I found this post and put the following code into Controller.php:

function init(){ 
$cssFile = 'default.css'; 
case 'test': $cssFile = '1.css'; break; 
case 'contact': $cssFile = '2.css'; break; 
// etc. 


However, back on the site if I create a new page "test" (url=test, title=test, navigation=test) the 1.css file is definitely not called. What am I doing wrong?

Also, is it possible to use wildcards so that any page with "test" in its title/url, ie. "test-page1" or "test-ing" will call the 1.css file?

Many thanks in advance.


Community Member, 35 Posts

25 February 2009 at 7:50pm

Edited: 25/02/2009 7:50pm

To me, this seems like the wrong way of doing things - I think what you should really be doing is defining subclasses of Page, then requiring different CSS for each one. This will also give you the opportunity to add more changes to the different sections later on.


Community Member, 22 Posts

26 February 2009 at 6:30pm

Thanks. I had come to a similar conclusion.

I've used this method:

Create an Enum field on the page class:

static $db = array( 
"PageStyle" => "Enum('Red,Green,Blue')", 

function getCMSFields() { 
$fields = parent::getCMSFields(); 
$fields->addFieldToTab("Root.Content.Main", new DropdownField("PageStyle", "Style", array( 
"Red" => "Red", 
"Green" => "Green" 
"Blue" => "Blue", 
return $fields; 

Then in the template 
<body class="style-$PageType"> 

which was suggested in this earlier forum post.