Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » ParentID if lowerlevel pages, ID if not? Help!

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2973 Views
  • dancrew32
    Avatar
    Community Member
    15 Posts

    ParentID if lowerlevel pages, ID if not? Help! Link to this post

    trying to make a different header image for specific pages based on their parent id..

    the problem is that When you're on the ParentID page, it outputs '0' for a value.

    How would I make it write it's own ID (when on the top level page) and write it's ParentID when you're on a child page?

    id="header" class="ban$ParentID"

    Thanks in advance!

  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: ParentID if lowerlevel pages, ID if not? Help! Link to this post

    That is going to get broken pretty quick if you have to keep making classes for each page.

    Have you thought about using a enum select for pages to select from a defined list of classes.. you could even use a dataobject list to setup new classes to make sure it is kept current.

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: ParentID if lowerlevel pages, ID if not? Help! Link to this post

    dancrew: I recommend that you make a little function on your Page class, HeaderClass:

    it could be this:

    function HeaderClass() {
    if($this->ParentID) return "banner-" . $this->ParentID;
    else return "banner-" . $this->ID;
    }

    But Blackdog's suggestion is better. For that, you could make an enum field called HeaderClass instead of the HeaderClass() function.

  • dancrew32
    Avatar
    Community Member
    15 Posts

    Re: ParentID if lowerlevel pages, ID if not? Help! Link to this post

    Sam and Blackdog, I really appreciate the help.

    Sam, I am new to this MVC deal, and I'm curious where you might implement the script you provided below. Would it be in mysite/code/page.php? How would I call it on my theme/page.ss?
    $HeaderClass?

    Sorry, new to this part of SS still. Thanks for any help you can provide. =]

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: ParentID if lowerlevel pages, ID if not? Help! Link to this post

    Yes on both counts. HeaderClass() should either be inside class Page or class Page_Controller.

    If it's inside class Page, then you will be able to access it inside a block such as <% control Menu(1) %> - getting the HeaderClass for each of the menu items. However, I'm sure not that you would ever want to do this.

    If you put it inside class Page_Controller, then you will only be able to access it at the top level. For methods that don't make any sense to be used inside a <% control %> block, we usually put them on the controller.

  • dancrew32
    Avatar
    Community Member
    15 Posts

    Re: ParentID if lowerlevel pages, ID if not? Help! Link to this post

    Sorry, hopefully this is the last question... I just don't quite get how to implement the control from Page_Controller. Would it be like this?

    <div id="header" class="<% control HeaderClass() %><% end_control %>">

    my page controller class:

    class Page_Controller extends ContentController {
       function init() {
          parent::init();
          
          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
       }
    function HeaderClass() {
    if($this->ParentID) return "banner-" . $this->ParentID;
    else return "banner-" . $this->ID;
    }
    }

    (btw, this doesn't output anything in the class="" section for some reason.. any ideas?)

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: ParentID if lowerlevel pages, ID if not? Help! Link to this post

    You're heading in the right direction.

    Take the parenthesis off the <% control HeaderClass() %> inside the template, that isn't required and may be breaking. Also, you should be just returning $HeaderClass, as it's a single piece of text. The <% control HeaderClass %> block is used for a set of data returned, as opposed to a single string.

    e.g.

    <div id="header" class="$HeaderClass">

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