Jump to:

23023 Posts in 11882 Topics by 2830 members

General Questions

SilverStripe Forums » General Questions » Howto get the page Level?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2065 Views
  • patte
    Avatar
    Community Member
    63 Posts

    Howto get the page Level? Link to this post

    Hi guys,

    does someone know a way to get the current page Level? I want to include different templates in hierarchical depency.

    Any Idea?

    Thanks much!
    patte

  • UncleCheese
    Avatar
    4085 Posts

    Re: Howto get the page Level? Link to this post

    I'm surprised there doesn't seem to be a built in function for this. You could probably do something like this

    public function CurrentLevel() {
    $page = $this;
    $level = 1;
    while(1) {
    if($page->Parent) {
    $level++;
    $page = $page->Parent();
    }
    else {
    return $level;
    }
    }
    }

  • patte
    Avatar
    Community Member
    63 Posts

    Re: Howto get the page Level? Link to this post

    Hi UncleCheese,

    thank you very much - works like a charm ! I am really surprised too that SilverStripe do not provide this function per default...

    You saved my day ;-)

    Patte

  • SuperStromboli
    Avatar
    Community Member
    3 Posts

    Re: Howto get the page Level? Link to this post

    This is good to know. I had multiple levels of nested pages and only wanted to display breadcrumbs if the page had a parent so I added this to my controller:

    public function IsSubPage() {
          return ($this->Parent);   
    }

    Worked great.

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Howto get the page Level? Link to this post

    @SuperStromboli that functionality is available in the templates - You should be able to do <% if Parent %>

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