Java 如何通过freemarker模板中的索引获取列表项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29508541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get list items by index in freemarker template?
提问by Rasool Ghafari
Is there a way to get list item by index in freemarker template, maybe something like this:
有没有办法在 freemarker 模板中按索引获取列表项,可能是这样的:
<#assign i = 1>
${fields}[i]
i'm new to freemarker.
我是 freemarker 的新手。
采纳答案by Duffmaster33
Yes, you can easily use the index to get at an item like ${fields[i]}
. You might want to loop over the indexes using something like:
是的,您可以轻松地使用索引来获取像${fields[i]}
. 您可能希望使用以下内容循环索引:
<#list 0..fields?size-1 as i>
${fields[i]}
</#list>
Alternatively, you can just list over a sequence without the index like:
或者,您可以只列出没有索引的序列,例如:
<#list fields as field>
${field}
</#list>
回答by manoj kumar c.a
you can use inbuilt index property of FMT: eg:
您可以使用 FMT 的内置索引属性:例如:
<#list ['a', 'b', 'c'] as i> ${i?index}: ${i}
<#list ['a', 'b', 'c'] as i> ${i?index}: ${i}