Get list from list in freemarker
If you really want to make this index-based slice:
<#assign listVar = ["v1", "v2", "v3", "v4" ] /> <#assign sublistVar = listVar[0..1] /> But be careful, it will stop with an error if the index is out of range. Depending on what you need it for, you can use ?chunk(2) instead.
Update:. As well as to avoid indexing errors outside the limits, in FreeMarker 2.3.21 you can release listVar[0..*2] , which cuts out 2 elements or less if there are fewer. (Exclusive processing may also be required: listVar[0..<2] )
When listing a sequence, you can use the index variable.
<#assign listVar = ["v1", "v2", "v3", "v4" ] /> <#list listVar as aVar> <#if aVar_index > 2><#break/></#if> </#list> You can also break the sequence using chunk . This will split the sequence into several sequences of a given size.
<#assign partitions = listVar?chunk(2) /> <#assign firstPartition = partitions?first /> Source: FreeMarker Guide
However, itβs better to filter the data before passing it to the template.