Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SilverStripe 3 - Options for <% loop %>

Is there a list of all the options I can add to a loop?

I don't know if options is the right name for it. I mean these

<% loop Dataobject.Reverse %>
<% loop Dataobject.Limit() %>

Can someone tell me everything that's possible here? And what the correct name for it is?

like image 265
invictus Avatar asked Jan 11 '23 17:01

invictus


1 Answers

there is an error in your question, it is <% loop $DataList.xxx %> or <% loop $ArrayList.xxx %> (see, you are looping a list of DataObjects)

well, loop is basically just a foreach loop

so, for example:

<% loop $DataList.Reverse %>$Title<% end_loop %>

is kindof the same as:

<?php 
foreach($dataList->reverse() as $item) { 
    echo $item->Title; 
}

'kindof' the same, because in fact the template does some checking and casting for you (eg it does not throw and error if Title is not set), and a loop can only loop SilverStripe lists, not arrays


tl;dr; / the conclusion

loop has no options at all
the options that you speak of are methods that exist on the list that you want to loop. the 2 lists php classes that you would normally loop are:

  • DataList api docs for class DataList
  • ArrayList api docs for class ArrayList

see the list of methods in the API docs for what methods are available.

obviously not all methods are meant to be used to loop,
only those that return a DataList or ArrayList will be useful.
you can see what they return from the first column of the table.

for example:

public ArrayList limit( integer $length, integer $offset = 0 )

means:

  • it is public (so its accessable, private or protected ones will not be available in template)
  • it returns ArrayList
  • the name is limit
  • the parameters are a number length and an number offset

so you can do: <% loop $List.limit(10,5) %>


further reading:

some methods in that list do not show parameters but actually do have them, this is because they are dynamic and the API docs fail to display that.

example:

public ArrayList filter( )

can be used like this (I think, never tried it):

<% loop $List.filter('Name', 'Zauberfisch') %>

you can also add your own methods by creating an Extension and adding this Extension to DataList and ArrayList

like image 149
Zauberfisch Avatar answered Jan 16 '23 18:01

Zauberfisch