Jump to:

3444 Posts in 1030 Topics by 871 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: 412 Views
  • tee 1977
    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


  • Willr
    Forum Moderator
    5508 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

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.