Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Adding variable .css files

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

Page: 1
Go to End
Author Topic: 1641 Views
  • redcirce
    Avatar
    Community Member
    22 Posts

    Adding variable .css files Link to this post

    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 this post 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.

  • alirobe
    Avatar
    Community Member
    35 Posts

    Re: Adding variable .css files Link to this post

    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.

  • redcirce
    Avatar
    Community Member
    22 Posts

    Re: Adding variable .css files Link to this post

    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.

    1641 Views
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.