Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of String in Freemarker

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

like image 737
Lily Avatar asked Apr 17 '09 14:04

Lily


People also ask

How do you create a list on FreeMarker?

FreeMarker doesn't support modifying collections. But if you really want to do this in FreeMarker (as opposed to in Java), you can use sequence concatenation: <#assign myList = myList + [newItem]> . Here you create a new sequence that wraps the two other sequences.

Does FreeMarker have content?

The has_content FunctionFreeMarker has built-in functions to detect for variables. The most common method of detecting for empty or null values uses the has_content function and the trim function. The has_content function is often used with another built-in FreeMarker function to detect for null values.

How do you append strings in FreeMarker?

$( document ). ready(function() { var html = ${itemsToAppendTo} $('. gTA'). append( html ) });


2 Answers

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

like image 178
NickGreen Avatar answered Oct 07 '22 16:10

NickGreen


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

For example, the template:

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

${colors?join(", ")}

.. will generate:

red, green, blue

like image 30
Vincent Cantin Avatar answered Oct 07 '22 18:10

Vincent Cantin