Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freemarker, list index and count condition

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?

like image 336
Will Hancock Avatar asked Apr 13 '12 11:04

Will Hancock


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.

What syntax is used to access data using FreeMarker under a condition?

Special variables are variables defined by the FreeMarker engine itself. To access them, you use the . variable_name syntax.

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.

Is FreeMarker case sensitive?

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.


1 Answers

Try with item_has_next

In your example:

<#if pages_has_next>,</#if> 
like image 193
darijan Avatar answered Sep 22 '22 02:09

darijan