List of String in Freemarker

Lily picture Lily · Apr 17, 2009 · Viewed 24.7k times · Source

I have a list of string in java code:

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

and I would like to display the keywords using 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

Banana

Answer

NickGreen picture NickGreen · Feb 23, 2012

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