更新时间:2023-12-04 20:31:52
如果您真的想基于索引制作该切片:
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]
)