Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Howto get the page Level?


Go to End


5 Posts   4727 Views

Avatar
patte

Community Member, 63 Posts

23 July 2010 at 10:21pm

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

24 July 2010 at 6:41am

Edited: 24/07/2010 6:42am

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;
}
}
}

Avatar
patte

Community Member, 63 Posts

25 July 2010 at 12:04am

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

Avatar
SuperStromboli

Community Member, 3 Posts

25 May 2011 at 7:42am

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.

Avatar
Willr

Forum Moderator, 5523 Posts

25 May 2011 at 5:35pm

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