21293 Posts in 5733 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1195 Views |
-
Nested menus question

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 "{$Title}" 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 "{$Title}" 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.
-
Re: Nested menus question

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).
-
Re: Nested menus question

24 November 2009 at 7:43am Last edited: 24 November 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.
-
Re: Nested menus question

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.
| 1195 Views | ||
|
Page:
1
|
Go to Top |


