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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:08:05  来源:igfitidea点击:

How to get list items by index in freemarker template?

javatemplatesfreemarkertemplate-engine

提问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}

回答by Eddy

Tested online, the following works well.

在线测试,以下效果很好。

Input:

输入:

someList = ["2019-12-16", 3]

Template:

模板:

<ul> 
   <li>${someList[0]}</li>
   <li>${someList[1]}</li>
</ul>

Output:

输出:

<ul> 
   <li>2019-12-16</li>
   <li>3</li>
</ul>