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.

Template Questions /

Reverse not working


Reply


3 Posts   169 Views

Avatar
SilverPeed

Community Member, 12 Posts

1 July 2014 at 3:48am

Hallo, Im using Silverstripe 3.1.5

http://doc.silverstripe.org/framework/en/reference/templates
In the URL above is explained that you can loop thru items in reverse order but it does not seem to work.
I do not see what i'm doing wrong here.
This is my code.

<% loop GastenboekItems.Reverse %>
      $Content
      <div class="arrangement">
         <div class="bericht-info"><p>$Datum<p><h4>Naam: $Naam</h4></div>
         $Bericht
      </div>
<% end_loop %>

Avatar
Devlin

Community Member, 215 Posts

1 July 2014 at 7:43pm

Edited: 01/07/2014 7:52pm

Works for me. But you need to define a sort first to reverse the sort.

<% loop $GastenboekItems.Sort(Naam).Reverse %>
   $Naam
<% end_loop %>

... which of course would be same as $GastenboekItems.Sort(Naam DESC)

Or:

public function GastenboekItems() {
   return GastenboekItems::get()->sort('Naam');
}

<% loop $GastenboekItems.Reverse %>
   $Naam
<% end_loop %>

Or you use $default_sort.

class GastenboekItems extends DataObject {
   private static $default_sort = 'Naam ASC';
}

<% loop $GastenboekItems.Reverse %>
   $Naam
<% end_loop %>

Avatar
SilverPeed

Community Member, 12 Posts

3 July 2014 at 12:53am

Thanks a lot, it works.

And the example of the function gives me a better understanding how to create your own functions.

Many thanks