From the lis...">

Get list from list in freemarker

I have a list in a free marker, as shown below:

<#assign listVar = ["v1", "v2", "v3", "v4" ] /> 

From the list above, I just want the subscriptions to be both v1 and v2.

I wandered to get an extra list in the free marker. But could not be found.

Any help would be appreciated.

+6
source share
2 answers

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] )

+7
source

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.

+1
source

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


All Articles