Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Building menu with children of current page

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

Page: 1
Go to End
Author Topic: 1385 Views
  • designerdre
    Avatar
    Community Member
    19 Posts

    Building menu with children of current page Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Building menu with children of current page Link to this post

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

  • dre
    Avatar
    Community Member
    1 Post

    Re: Building menu with children of current page Link to this post

    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.

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