Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get sub list from a list in freemarker

Tags:

freemarker

I have a list in free marker as below:

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

From the above list I just want the sub list as v1 and v2.

I have been wandering to get the sub list in free marker. But couldn't managed to find.

Any help would be appreciate.

like image 902
venky Avatar asked Aug 27 '13 06:08

venky


1 Answers

If you really want to make that slice based on indexes:

<#assign listVar = ["v1", "v2", "v3", "v4" ] />
<#assign sublistVar = listVar[0..1] />

See Freemarker Sequence slicing.

But beware, it will stop with error if the index is out of range. Depending on what you need this for, you may want to use ?chunk(2) instead.

Update: As of avoiding index-out-of-bounds error, in FreeMarker 2.3.21 you can issue listVar[0..*2], which will slice out 2 items, or less if there's less available. (Also exclusive-end slicing can come handy: listVar[0..<2])

like image 54
ddekany Avatar answered Sep 22 '22 15:09

ddekany