从freemarker的列表中获取子列表 [英] Get sub list from a list in freemarker
问题描述
我在免费标记中有一个列表,如下所示:
I have a list in free marker as below:
<#assign listVar = ["v1", "v2", "v3", "v4" ] />
从上面的列表中,我只希望子列表为v1和v2.
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.
任何帮助将不胜感激.
推荐答案
如果您真的想基于索引制作该切片:
If you really want to make that slice based on indexes:
<#assign listVar = ["v1", "v2", "v3", "v4" ] />
<#assign sublistVar = listVar[0..1] />
请参见 Freemarker序列切片.
但是请注意,如果索引超出范围,它将以错误停止.根据您需要的用途,可能要改用?chunk(2)
.
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.
更新:为避免出现索引越界错误,在FreeMarker 2.3.21中,您可以发出listVar[0..*2]
,它将切出2个项目,如果可用空间较小,则减少两个项目. (也可以使用专用端切片:listVar[0..<2]
)
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]
)
这篇关于从freemarker的列表中获取子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!