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.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SS 3.0.3 - Loop children not limited to children of current page - SOVLED


Go to End


5 Posts   7317 Views

Avatar
Thomashv

Community Member, 35 Posts

22 December 2012 at 4:20am

I'm testing out SilverStripe 3.0.3, and I find the side behaving strange. I see that the code has changed in the simple template from using <% loop menu(2) %> to using <% loop $Children %>

Anyway, the result is that I get all children of every page showing up on everypage with children. Is there any bugs in the built in template controls? I mean loop children should normally only loop through the children of the specific page?

This is the new SidebarMenu simple template code:


<%--Include SidebarMenu recursively --%>
<% if $Children %>
	<% loop $Children %>
		<li class="$LinkingMode">
			<a href="$Link" class="$LinkingMode" title="Go to the $Title.XML page">
				<span class="arrow">&rarr;</span>
				<span class="text">$MenuTitle.XML</span>
			</a>

			<% if $Children %>
				<ul>
					<% include SidebarMenu %>
				</ul>
			<% end_if %>

		</li>
	<% end_loop %>
<% end_if %>

Avatar
Thomashv

Community Member, 35 Posts

22 December 2012 at 12:58pm

Edited: 22/12/2012 12:59pm

I found that in the SideBar.ss include it is defined a loop through $Menu(1) for the SidebarMenu.ss include. Like this:


<aside>
	<% if $Menu(2) %>
		<nav class="secondary">
			<h3>
				<% loop $Level(1) %>
					$Title
				<% end_loop %>
			</h3>
			<ul>
				<% loop $Menu(1) %>
					<% include SidebarMenu %>
				<% end_loop %>
			</ul>
		</nav>
	<% end_if %>
</aside>

That is why the sidebar menu shows the children of all of the first level pages instead of showing only the children of the current page as we are used to in SilverStripe. I don't know if this are some kind of a new idea or just a mistake, but to get it work as I intended I just deleted the loop around the <% include SidebarMenu %>. Just like this:


<aside>
	<% if $Menu(2) %>
		<nav class="secondary">
			<h3>
				<% loop $Level(1) %>
					$Title
				<% end_loop %>
			</h3>
			<ul>
				<% include SidebarMenu %>
			</ul>
		</nav>
	<% end_if %>
</aside>

Hope this could be to any help for other confused SilverStripers.

Merry Christmas to all of you!

Avatar
dayhox

Community Member, 2 Posts

17 January 2013 at 6:17am

Thank you for this!
d

Avatar
Bruce B

Community Member, 164 Posts

17 January 2013 at 6:57pm

Just to clarify: this thread is referring to the Simple theme that is included in SS 3.
The code that Thomashv provided is close but not quite there. It won't provide menus when you are in the child pages. The following code for SideBar.ss has been posted in github and should be in the next version. Meanwhile, here it is for anyone who has found this thread

<aside>
<% if $Menu(2) %>
<nav class="secondary">
<% with $Level(1) %>
<h3>
$Title
</h3>
<ul>
<% include SidebarMenu %>
</ul>
<% end_with %>
</nav>
<% end_if %>

</aside>

Avatar
Thomashv

Community Member, 35 Posts

16 May 2013 at 7:43am

And then a half year almost was gone... But thank you very much for the clearification. :)