How to get list items by index in freemarker template?
34,255
Solution 1
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>
Solution 2
you can use inbuilt index property of FMT: eg:
<#list ['a', 'b', 'c'] as i> ${i?index}: ${i} </#list>
Solution 3
Tested online, the following works well.
Input:
someList = ["2019-12-16", 3]
Template:
<ul>
<li>${someList[0]}</li>
<li>${someList[1]}</li>
</ul>
Output:
<ul>
<li>2019-12-16</li>
<li>3</li>
</ul>
![Rasool Ghafari](https://i.stack.imgur.com/7DHAX.png?s=256&g=1)
Author by
Rasool Ghafari
Updated on September 28, 2021Comments
-
Rasool Ghafari over 2 years
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.