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

Reverse not working


Go to End


3 Posts   2270 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