Freemarker, list index and account status

I am writing a JS array of objects in a Freemarker template. But I am having serious problems, not including the comma after the last element.

<#assign pages = module.pages.page> wh.pages = [ <#list pages as page> {"name" : "${ page.@name }", "href" : "${ page.@href }"} <#if (index+1) < pages?size>,</#if> </#list> ] 

Therefore, while repeating the list, while the index + 1 is less than the length / size of the variable pages, it should write a comma. So when it is equal in size, it should omit the comma.

So how can this be achieved?

+6
source share
2 answers

The pointer must be prefixed with the name of your element. In the example:

 <#if (index+1) < pages?size>,</#if> 

should have been:

 <#if (page_index+1) < pages?size>,</#if> 
+8
source

Try it with item_has_next

In your example:

 <#if pages_has_next>,</#if> 
+12
source

Source: https://habr.com/ru/post/913123/


All Articles