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

Nested menus question


Go to End


4 Posts   1828 Views

Avatar
Romeo

Community Member, 67 Posts

24 November 2009 at 6:54am

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.

Avatar
bummzack

Community Member, 904 Posts

24 November 2009 at 7:35am

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

Avatar
bummzack

Community Member, 904 Posts

24 November 2009 at 7:43am

Edited: 24/11/2009 7:44am

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.

Avatar
Romeo

Community Member, 67 Posts

24 November 2009 at 10:06am

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