1

I'm putting together a 3 tier navigation menu in a SilverStripe 3.1 template, and have the following code in my template:

<% loop $Menu(1) %>
...
  <% loop $Children %>
...
    <% loop $Children %>
     <li><a href="$Link">$Model</a></li>
    <% end_loop %>
  <% end_loop %>
<% end_loop %>

However I'm not getting the output I expect from the 3rd tier. Is it actually possible to get the Children of the Children? If not, then what should I do instead? Thanks!

Highly Irregular
  • 38,000
  • 12
  • 52
  • 70
  • That code looks OK. You should be able to loop through the Children of a Children loop like you are doing. Are all the pages all set to `ShowInMenus`? `$Children` only returns pages that are set to `ShowInMenus`. Otherwise you can use `$AllChildren` to get hidden pages as well. Does the site tree have pages that are 3 levels deep? What is `$Model`? Is this a custom variable that you have added to your Page class? – 3dgoo Apr 29 '15 at 04:32
  • @3dgoo Yes, all the pages I'm wanting to appear have ShowInMenus set correctly. Yes, there are pages 3 levels deep. And yes, $Model is a custom variable. – Highly Irregular Apr 29 '15 at 04:38
  • @3dgoo looking into it further, it looks like I'm getting the value of $Link but not model, so I just need to investigate the custom variable. Thanks for your help; would you like to move your comment to be an answer? – Highly Irregular Apr 29 '15 at 04:40
  • Glad I could help. Sure, I'll post my comments as an answer. – 3dgoo Apr 29 '15 at 04:42
  • When you find your problem make sure you share your solution in case it helps anyone in the future. – 3dgoo Apr 29 '15 at 05:45

1 Answers1

3

Yes it is possible to loop through the Children of a Children loop.

Your code looks correct to me. It should work correctly.

Here are a few possible issues to check.

Make sure all the pages at each level have ShowInMenus set to true. $Children and $Menu(1) only returns pages that have ShowInMenus set to true. This checkbox can be found in the Settings tab of any page. Otherwise you can use $AllChildren instead of $Children to get hidden pages as well.

Make sure the site tree has pages that are 3 levels deep. An obvious thing to check.

$Model is not an in built page variable. This must a custom variable you have set. Make sure this is set to the Page, has values filled in and is accessible on the front end.

Here is some test template code you can use to check the output of your site tree. This may help you in debugging your problem:

<ul>
<% loop $Menu(1) %>
    <li>
        <a href="$Link">$Title - $Model</a>
        <% if $Children %>
        <ul>
        <% loop $Children %>
            <li>
                <a href="$Link">$Title - $Model</a>
                <% if $Children %>
                <ul>
                <% loop $Children %>
                    <li>
                        <a href="$Link">$Title - $Model</a>
                    </li>
                <% end_loop %>
                </ul>
                <% end_if %>
            </li>
        <% end_loop %>
        </ul>
        <% end_if %>
    </li>
<% end_loop %>
</ul>
3dgoo
  • 15,716
  • 6
  • 46
  • 58