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
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.
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.
$( document ). ready(function() { var html = ${itemsToAppendTo} $('. gTA'). append( html ) });
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With