Jump to:

23455 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Nested menus question

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: 1370 Views
  • Romeo
    Avatar
    Community Member
    67 Posts

    Nested menus question Link to this post

    I have a simple site where some pages are actually just containers of sub-pages. When one of these main pages is selected in the main navigation menu (which is in a sidebar), I actually show the first subpage, using this technique in the Page subclass:

    $url=Director::baseURL().$this->Children()->First()->URLSegment;
    Director::redirect($url);

    I also display on the page a submenu of the subpages, so the user can move among them, with the current page selected. All has worked well so far. Now I want to move this submenu to be nested within the main menu item, so that when the user clicks a page with subpages, the submenu appears below the main page item. But it's not working for me. Before I had basically this:

    Main nav menu:

    <% control Menu(1) %>
    <li class="$LinkingMode">
    <% if LinkingMode = link %>
    <a href="$Link" title="Go to the &quot;{$Title}&quot; page">$MenuTitle</a>
       <% else %>
        <span>$MenuTitle</span>
       <% end_if %>
    </li>
    <% end_control %>

                      
    For the submenu:

    <% if Menu(2) %>
       <div id="Submenu">
        <ul id="Menu2">
          <% control Menu(2) %>
           <li class="$LinkingMode">
                 <% if LinkingMode = current %>
                   <span>$MenuTitle</span>
                <% else %>
                   <a href="$Link" title="Go to the $Title page">$MenuTitle</a>
                <% end_if %>

           </li>
          
          <% end_control %>
        </ul>
    </div>
    <% end_if %>

    This was working. What doesn't work is this:

    <% control Menu(1) %>
    <li class="$LinkingMode">
    <% if LinkingMode = link %>
    <a href="$Link" title="Go to the &quot;{$Title}&quot; page">$MenuTitle</a>
       <% else %>
        <span>$MenuTitle</span>
       <% end_if %>
    <% if Menu(2) %>
        <div id="Submenu">
        <ul id="Menu2">
           <% control Menu(2) %>
           <li class="$LinkingMode">
                  <% if LinkingMode = current %>
                    <span>$MenuTitle</span>
                 <% else %>
                    <a href="$Link" title="Go to the $Title page">$MenuTitle</a>
                 <% end_if %>

           </li>
          
           <% end_control %>
        </ul>
    </div>
    <% end_if %>
    </li>
    <% end_control %>

    That is, with the submenu nested within the parent li. Nothing shows up in the output. So what's different, what do I need to do to get this to work? I'm sure it's something basic to do with my understanding of levels.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Nested menus question Link to this post

    Use Children instead of Menu(2). In the Menu(1) control, you're basically in the scope of each Menu(1) item.. therefore you should check for Children of that item and not Menu(2) which is meant to be used globally (eg. separated navigations).

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Nested menus question Link to this post

    Another Tip: I suggest you override the Link method of your Subpage to directly output a link to the first child. This way you can avoid unnecessary redirects (better for search engines and the like).
    Example:

    function Link() {
       if($this->Children()->First())
          return $this->Children()->First()->Link();
       else
          return parent::Link();
    }

    You could also use the built in RedirectorPage or look at its code for inspiration.

  • Romeo
    Avatar
    Community Member
    67 Posts

    Re: Nested menus question Link to this post

    Thanks ever so much for that, it solved my problem nicely. Thanks too for the Link tip, which also works well.

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