I'm writing a JS Array of objects in a Freemarker template. But I am having serious issues not including a comma after the last item.
<#assign pages = module.pages.page>
wh.pages = [
<#list pages as page>
{"name" : "${page.@name}", "href" : "${page.@href}"}
<#if (index+1) < pages?size>,</#if>
</#list>
]
So during the list repeat, while index + 1 is less than the length/size of the pages variable, it should write a comma. So that when it equals the size, it should omit the comma.
So how can this be achieved?
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.
Special variables are variables defined by the FreeMarker engine itself. To access them, you use the . variable_name syntax.
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.
The string converted to boolean value. The string must be true or false (case sensitive!), or must be in the format specified by the boolean_format setting. If the string is not in the appropriate format, an error will abort template processing when you try to access this built-in.
Try with item_has_next
In your example:
<#if pages_has_next>,</#if>
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