SilverStripe 3 - Options for <% loop %>

匆匆过客 提交于 2019-12-04 11:33:47

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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!