Jump to:

3397 Posts in 978 Topics by 854 members

Template Questions

SilverStripe Forums » Template Questions » giving a class to a section on a website

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

Page: 1
Go to End
Author Topic: 326 Views
  • tee 1977
    Avatar
    Community Member
    6 Posts

    giving a class to a section on a website Link to this post

    Hi, im hoping someone out there can help me.

    Basically i need to assign a different style class depending on the section you're on in the site and all its children pages need to follow this style. Normally if i wanted to assign a class to a page i would use body class="$URLSegment" etc. This is fine if you need one or two pages to look different but i need the parent and its children to follow the same class so i can color code text elements and it follows suit.

    Im just scratching my head to find an easy solution.

    many thanks

    Tamara

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: giving a class to a section on a website Link to this post

    Most of the time if I need section specific styles I'll add a new field on Page.php called 'Style' with that being configurable in the cms. Relaying on urlsegments is a little risky depending on the client!

    class Page extends SiteTree {

    static $db = array(
    'Style' => 'Enum("Blue, Green, Red, Inherit", "Inherit");
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.Main', new DropdownField('Style', 'Style', $this->dbObject('Style')->enumValues()));

    return $fields;
    }

    function getPageStyle() {
    if($this->Style == "Inherit") {
    if($parent = $this->Parent()) {
    return $parent->getPageStyle();
    }
    else {
    return "Blue";
    }
    }

    return $this->Style;
    }

    Then use $PageStyle in your template

    326 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.