How to get list items by index in freemarker template?

Rasool Ghafari picture Rasool Ghafari · Apr 8, 2015 · Viewed 27.6k times · Source

Is there a way to get list item by index in freemarker template, maybe something like this:

<#assign i = 1>
${fields}[i]

i'm new to freemarker.

Answer

Duffmaster33 picture Duffmaster33 · Apr 16, 2015

Yes, you can easily use the index to get at an item like ${fields[i]}. You might want to loop over the indexes using something like:

<#list 0..fields?size-1 as i>
${fields[i]}
</#list>

Alternatively, you can just list over a sequence without the index like:

<#list fields as field>
${field}
</#list>