Foros de discusión

How to iterate array randomly in Velocity Template?

Ronak Parekh, modificado hace 8 años.

How to iterate array randomly in Velocity Template?

Junior Member Mensajes: 76 Fecha de incorporación: 6/04/15 Mensajes recientes
Hi Team,

I want to get value of repeatable structure field in velocity template randomly.
So I wanted to know that, how I can achieve random shuffling of an array in velocity template?
Please provide any help.

Thanks & Regards,
Ronak Parekh
thumbnail
James Falkner, modificado hace 8 años.

RE: How to iterate array randomly in Velocity Template? (Respuesta)

Liferay Legend Mensajes: 1399 Fecha de incorporación: 17/09/10 Mensajes recientes
Use Collections.shuffle. Here's an example using a structure with a single repeatable textfield named name:

#set ($coll = $portal.getClass().forName('java.util.Collections'))
#set ($randomSiblings = $name.siblings)
#set ($V = $coll.shuffle($randomSiblings))

#foreach ($el in $randomSiblings)
<p>Random Sibling: $htmlUtil.escape($el.data)</p>
#end
Ronak Parekh, modificado hace 8 años.

RE: How to iterate array randomly in Velocity Template?

Junior Member Mensajes: 76 Fecha de incorporación: 6/04/15 Mensajes recientes
Hi James Falkner,

Thanks a lot. That solved my problem.

Thanks & Regards,
Ronak Parekh