Search code examples
javatemplatesfreemarkertemplate-engine

How to get list items by index in freemarker template?


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.


Solution

  • 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>