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.

Data Model Questions /

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

How to Sort Includes using a loop


Go to End


5 Posts   1391 Views

Avatar
whjonker

Community Member, 12 Posts

2 March 2017 at 3:26am

Edited: 02/03/2017 3:26am

Hi,

I am currently working on a one page website. I would like to order the different content blocks in the cms. So I made for all page types an Integer field "OrderHomePage" where I can set the order of the section.

On the homepage (homepage.ss) I have included all Page Types and added a Loop. I was expecting that the includes would in the correct order according to the value that was set for "OrderHomePage", but this does not work. Is there another easy way how to sort these sections?

<% loop $Children.Sort(OrderHomePage, ASC) %>

    <% if $SliderHomePageCheck == "1" %>
  	<% include SliderPage %>
    <% end_if %>
    
  	<!--************************************
				Main Start
  	*************************************-->
 	 <main id="tg-main" class="tg-main tg-haslayout"> 
  
  	<% if $NewsHomePageCheck == "1" %>
    <!--Start News Page with general info!-->
    <% include NewsPage %>
    <!--End NewsPage with general info!--> 
	<% end_if %>    
    
	<% if $EventHomePageCheck == "1" %>
    <!--Start Event Page with general info!-->
    <% include EventPage %>
    <!--End Event Page with general info!--> 
    <% end_if %>
    
	<% if $EventDetailHomePageCheck == "1" %>
    <!--Start Event Page with detailed info!-->
    <% include EventDetailPage %> 
    <!--End Event Page with detailed info!--> 
    <% end_if %>
    
	<% if $MatchMakingHomePageCheck == "1" %>
    <!--Start Match Making Page!-->
    <% include MatchMakingPage %>
    <!--End Match Making Page!--> 
    <% end_if %>
    
	<% if $MediaTeaserHomePageCheck == "1" %>
    <!--Start Media Teaser!-->
    <% include MediaTeaserPage %>
    <!--End Media Teaser!--> 
    <% end_if %>
    
	<% if $FighterHomePageCheck == "1" %>
    <!--Start Fighter Page!-->
    <% include FighterPage %>
    <!--End Fighter Page!--> 
    <% end_if %>
    
	<% if $FighterStatsHomePageCheck == "1" %>
    <!--Start Fighter Stats Page!-->
    <% include FighterStatsPage %>
    <!--End Fighter Stats Page!--> 
    <% end_if %>
    
	<% if $PartnerHomePageCheck == "1" %>
    <!--Start Partner Page!-->
    <% include PartnerPage %>
    <!--End Partner Page!--> 
    <% end_if %>

  </main>

  <% end_loop%>	

Avatar
dhensby

Community Member, 253 Posts

2 March 2017 at 5:02am

From first glance it looks like this should work. What aspect isn't working? Is there an error does the sort just not change?

You could start by debugging the SQL queries being made to see if the sort clause is being correctly added.

Alternatively you could make a function on your controller to return the ordered children rather than calling sort in the template.

Avatar
whjonker

Community Member, 12 Posts

2 March 2017 at 5:22am

Edited: 02/03/2017 5:22am

Hi dhensby,

None of the includes are currently being displayed.

The funny thing is when I do:

 <% control $Children.Sort(OrderHomePage, ASC) %>
	 <h1>What is the order</h1> $OrderHomePage <h2>$Title</h2>
<% end_control %>

It nicely outputs the Order and the Correct Page name. So the Sort seems to work, but somehow it does not sort the blocks with the includes of the Page Types.

Avatar
whjonker

Community Member, 12 Posts

3 March 2017 at 9:46pm

Is there anyone that can help me with this? I have been browsing weeks through the SS forum, but I am still not able to find a solution. It is probably very easy, but I am still learning.

Thank you!

Wesley

Avatar
Devlin

Community Member, 344 Posts

4 March 2017 at 5:16am

Edited: 04/03/2017 5:23am

If the control works but the loop doesn't, then maybe it's this typo in the end tag?

<% end_loop%>

edit: Nope. The space does not seem to matter for the loop.