Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » A page head for each section

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

Page: 1 2 3
Go to End
Author Topic: 6667 Views
  • Mans
    Avatar
    Community Member
    21 Posts

    A page head for each section Link to this post

    Hi!

    I'm new to SilverStripe, but so far I like it very much. I now have a development problem which I don't know quite how to solve (unfortunately the SS documentation seems to be far from complete).

    I have a site with six "sections" = six pages on Level 1 in the site structure. I want a distinct page head for each section, which would be inherited by the sub-pages of each respective section. For ease of maintenance I would like not having to create six different page types; I would rather have one page head which detects the section and replaces the shown contents accordingly.

    I tried using $URLSegment as body id and showing only the correct head by means of css. This worked fine on the top level, but as each sub-page get their own url this solution was not practical. I have also tried experimenting with Level(1) etc. but found no workable solution.

    Does anyone have a suggestion for a nice solution? I should also add that I am no programming genious -- I have dabbled in various simpler programming for a decade or two, but much of it is really over my head.

    Yours,
    MÃ¥ns

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: A page head for each section Link to this post

    my first thought was using different PageTypes, too - as you don't want to, things might be a little trickier.
    a solution i can think of is to add an attribute to your page classlike so

    static $db = array(
    "header" => "Enum('header1,header2,header3,header4,header5,header6,inherit','inherit')",
    );

    then populate the options in a dropdown in the cms:

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $field = new DropdownField(
    'header',
    'select header:',
    singleton('Page')->dbObject('header')->enumValues()
    );
    $fields->addFieldToTab('Root.Content.Main', $field);
    return $fields;
    }

    (see also http://doc.silverstripe.org/doku.php?id=dropdownfield)

    now, in you Page_Controller class, define a function to retrieve the header value in templates:

    function header() {
    if($this->header == 'inherit') {
    //some recursive function moving up the sitetree to detect the first page having header != 'inherit' and returning it
    //can't figure out the exact code for now... you might also like to leave this on out and just set 'header' on every page...
    } else {
    return $this->header;
    }
    }

    now use $header in your template as a css class as you mentioned ...

    hth

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: A page head for each section Link to this post

    There is a simpler way to do this. In your Page.php define whichever database fields you want for your header, in this example well just use a header image, but you can extend this as far as you need to.

    mysite/code/Page.php

    static $has_one = array (
    'HeaderImage' => 'Image'
    )

    function getCMS fields() {

    if(!$this->Parent){
    $fields->addFieldToTab("Root.Content.Main", new ImageField('HeaderImage'));
    }

    }


    then in your Page.ss template you can do this:

    <% control Level(1) %>
    $HeaderImage
    <% end_control %>

    The key to this is the if(!$this->Parent) which is checking that the page has no parents (i.e. is a root page) and if it is it adds the header field.

    The only problem with this method is that pages which do not extend 'Page' wont have the header field, an example of this is the Redirector page. You can get around this by extending the Redirector page and adding in the above code, or adding it to the origonal redirector page or even easier just set a fallback image for pages which don't have the header field.

    Anyway hope that helps

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: A page head for each section Link to this post

    you're completely right, aram, using <% Level(1) %> is the solution replacing my clumsy 'recursive function' approach.
    just wanted to mention, the post py 'Mans' didn't clearly mention it's all about a simple image - in case it's more a piece of html, the mentioned way of using css classes (or alternatively, using an HTMLText field) may do the trick.

  • Mans
    Avatar
    Community Member
    21 Posts

    Re: A page head for each section Link to this post

    Thanks a lot for the help, guys! It's great to know that there is so much knowledge in these forums. Turns out I went with the multiple PageType solution after all -- there is indeed more than an image in each head, and it tended to get a little too complex (for me) using other solutions.

    Again, thank you!

    /MÃ¥ns

  • martin_w
    Avatar
    Community Member
    28 Posts

    Re: A page head for each section Link to this post

    hello,

    @aram

    I exactly tried it with your way (the different header image thing).
    But it does not work.. when I upload my edited Page.php then I just get a white page.
    And also dbflush doesnt work.

    What could be the problem?

    --

    The code

    /**
        * header images
        */
       
       static $has_one = array (
          'HeaderImage' => 'Image'
       )
       
       function getCMS fields() {
       
          $fields = parent::getCMSFields();
       
          if(!$this->Parent){
          $fields->addFieldToTab("Root.Content.Main", new ImageField('HeaderImage'));
          }
       
       }

    The code is between
    class Page_Controller extends ContentController {
    ...
    }

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: A page head for each section Link to this post

    Hi Martin

    The getCMSFields needs to be in the Page model class (i.e. 'class Page extends SiteTree{}') not the controller, also there were a couple of typos in my post this should work:

    static $has_one = array (
    'HeaderImage' => 'Image'
    )

    function getCMSFields() {

    $fields = parent::getCMSFields();

    if(!$this->Parent){
    $fields->addFieldToTab("Root.Content.Main", new ImageField('HeaderImage'));
    }

    return $fields;
    }

  • martin_w
    Avatar
    Community Member
    28 Posts

    Re: A page head for each section Link to this post

    that works!

    thank you very much

    best regards, martin

    6667 Views
Page: 1 2 3
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.