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.

Archive /

Our old forums are still available as a read-only archive.

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

Recursive nested menu


Reply


3 Posts   2809 Views

Avatar
pointfiftyae

Community Member, 2 Posts

16 June 2008 at 5:26am

Edited: 16/06/2008 5:28am

Hi everyone!

I'm currently working on a project with Silverstripe where the client wants to be able to have a nested menu that works for all menu levels, i.e. it doesn't matter if there are 2 or 9 or n levels, they will all be displayed (the design allows for a fairly good amount of levels). All I've seen on the forums or the current themes are hard-coded menus of a fixed maximum depth.

I've tried this:

Nav.ss

<% control Menu(1) %>
<!-- here the code for the lists, links etc... -->
<% include SubNav %>
<% end_control %>

SubNav.ss
<% if Children %>
<% control Children %>
<!-- here the code for the lists, links etc... -->
<% if Children %>
<% include SubNav %>
<% end_if %>

<% end_control %>
<% end_if %>

The text in bold is supposed to get the SubNav file to include itself until there are no more children. The problem is that I get an exceeded memory error message. Obviously, the include statements are executed regardless of the ifs, resulting in an endless loop, as stated in this topic: http://silverstripe.com/site-builders-forum/flat/37299

Does anyone have the same problem, or maybe an idea on how to resolve this? Thank you very much in advance!

Avatar
saimo

Community Member, 67 Posts

16 June 2008 at 6:41am

What you will have to do until the flaw int the templating system is fixed is to do an implementation in php, and put it in Page.php. For example make a new function TreeMenu() and then call that from the template to get a nested tree of the pages. It's a bit ugly, but you could probably use the existing functions for controlling menus, and only use php to make it conditionally recursive.

Avatar
pointfiftyae

Community Member, 2 Posts

18 June 2008 at 8:01pm

Hey, thank you for your answer! I guess I will just write 5 or 6 navigation template files for now, and create that PHP function when I have time...