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.

Template Questions /

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

Building menu with children of current page


Go to End


3 Posts   3906 Views

Avatar
designerdre

Community Member, 19 Posts

20 December 2011 at 6:28am

Hey Guys,

I'm trying to find a good way to create a sub menu that will display links under 2 conditions:

1. If the current page is top level and has children, display links to the children pages.

2. If the current page is not a top level page but is nested under a top level page that has children then display links to the children of the current page's parent (the current page's siblings).

I understand how to build a navigation bar with drop down items based on the children of top level items but I can't figure out how to build a sub-menu in my template apart from the main navigation bar that will only display the children of the current page if they exist.

So in the case where a user visits the top level "About Us" page with the pages "History" and "Overview" as children, I'd like to detect that this current page has children and then display link to them. Or, in the case where the user visits either the "History" or "
Overview" sub pages, I'd like to detect that this page's parent has children and display links to all those children pages.

Really hoping someone here can give me some guidance on how to code this in the template.

Thanks in advance!

Avatar
Willr

Forum Moderator, 5523 Posts

20 December 2011 at 3:48pm

I recommend consulting the documentation on templates and try a couple things out (http://doc.silverstripe.org/sapphire/en/reference/built-in-page-controls). Level($num) is handy and <% control statements %>

In 2.4 you can use something lik
<% if Level(1) %>

<% if Children %>
<% control Children %>
// children of this page (1)
<% end_control %>
<% end_if %>

<% else_if Level(2) %>
<% control Parent %>
<% if Children %>
<% control Children %>
// siblings of this page (2)
<% end_control %>
<% end_if %>
<% end_control %>
<% end_if %>

Avatar
dre

Community Member, 1 Post

2 January 2012 at 5:08pm

Thanks for the helpful info Willr! I got it working using the following structure:

<% if Menu(2) %>
<% control Menu(2) %>

<% end_control %>
<% else %>

<% end_if %>

I feel like i tried this before and it did'nt work well but that did it. Thanks again and Happy New Year.