list Freemarker 中的字符串列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/760673/
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-09-11 01:21:05  来源:igfitidea点击:

List of String in Freemarker

listfreemarker

提问by Lily

I have a list of string in java code:

我在java代码中有一个字符串列表:

List<String> keywords = new ArrayList<String>();
keywords.add("Apple");
keywords.add("Banana");

and I would like to display the keywords using Freemarker: Apple, Banana

我想使用 Freemarker 显示关键字:Apple、Banana

How to do that?

怎么做?

PS: I read through the manual and found some articles suggesting using <#list>, but the output is: Apple

PS:我通读了手册,发现了一些建议使用的文章<#list>,但输出是:Apple

Banana

香蕉

采纳答案by idrosid

FreeMarker preserves your spaces (and EOL) but does not add any by itself. So, just put everything in the same line:

FreeMarker 会保留您的空间(和 EOL),但不会自行添加任何空间。因此,只需将所有内容放在同一行中:

<#list myListName as item>${item}</#list>

回答by NickGreen

If you want a comma-separated list, you can use the following:

如果你想要一个逗号分隔的列表,你可以使用以下内容:

<#list seq as x>
   ${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>  

see: http://freemarker.org/docs/ref_directive_list.html#pageTopTitle

请参阅:http: //freemarker.org/docs/ref_directive_list.html#pageTopTitle

回答by Vincent Cantin

Since the version 2.3.20 of Freemarker, there is a built-in command for comma-separated lists.

从 Freemarker 的 2.3.20 版本开始,有一个用于逗号分隔列表的内置命令

For example, the template:

例如,模板:

<#assign colors = ["red", "green", "blue"]>

${colors?join(", ")}

<#assign colors = ["red", "green", "blue"]>

${colors?join(", ")}

.. will generate:

.. 会产生:

red, green, blue

红、绿、蓝

回答by Erik Pragt

Since version 2.3.23, you can also use the following code:

从 2.3.23 版本开始,您还可以使用以下代码:

<#list users as user>
  <div>
    ${user}<#sep>, </#sep>
  </div>
</#list>

Taken from the sep directive.

取自sep 指令

回答by skaffman

Freemarker provides some features for whitespace control, see http://freemarker.sourceforge.net/docs/dgui_misc_whitespace.html

Freemarker 提供了一些空白控制的功能,参见http://freemarker.sourceforge.net/docs/dgui_misc_whitespace.html