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.

Template Questions /

Adding variable .css files


Reply


3 Posts   1701 Views

Avatar
redcirce

Community Member, 22 Posts

24 February 2009 at 12:14pm

Hello

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 [url=http://silverstripe.org/archive/show/217353]this post[/url] and put the following code into Controller.php:

function init(){
$cssFile = 'default.css';
switch($this->URLSegment){
case 'test': $cssFile = '1.css'; break;
case 'contact': $cssFile = '2.css'; break;
// etc.
}

Requirements::css($cssFile);
}

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.

Avatar
alirobe

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.

Avatar
redcirce

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 [url=http://silverstripe.org/archive/show/60032#post60032] this earlier forum post[/url].