Remember me? forgot password | register

SilverStripe Forum

10690 Posts in 2797 Topics by 1519 members

Jump to:

Site Builders

If this is your first visit, you will need to register before you can post. However, you can browse all messages below.

Community » SilverStripe Forum » Site Builders » ParentID if lowerlevel pages, ID if not? Help!
Page: 1 go to end Reply
Author Topic: ParentID if lowerlevel pages, ID if not? Help! 1027 views
  • dancrew32
    avatar
    Community Member
    14 posts
    3 SilverStripe Sites

    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
    Blackdog's avatar
    Community Member
    136 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
    Core Development Team
    482 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
    14 posts
    3 SilverStripe Sites

    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
    Core Development Team
    482 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
    14 posts
    3 SilverStripe Sites

    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?)

    Last edited: 6 May 2008 at 3:58am

  • Sean
    avatar
    Core Development Team
    435 posts
    1 SilverStripe Site 1 SilverStripe Theme

    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">

    Last edited: 6 May 2008 at 9:08am

    1027 views
go to top Reply

Currently Online:

eljefe, patrics, Aster, motoservo

Welcome to our latest member: patrics