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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Reverse not working


Go to End


3 Posts   1357 Views

Avatar
SilverPeed

Community Member, 22 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, 344 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, 22 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