How to get list items by index in freemarker template?

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

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

I am new to freemarker.

+8
source share
2 answers

Yes, you can easily use an index to get an element like ${fields[i]} . You might want to iterate over the indices using something like:

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

Alternatively, you can simply list the sequence without an index:

 <#list fields as field> ${field} </#list> 
+14
source

You can use the built-in FMT index property: for example:

<#list ['a', 'b', 'c'] as i> $ {i? index}: $ {i}

+1
source

Source: https://habr.com/ru/post/984834/


All Articles