Han
7 years agoAce
Add a separator if freemarker loop has more than one item
I am new to logic in freemarker and having trouble adding a separator in a loop if there is more than one item.
My current code below is adding a separator, but it appears after my items;
<#assign userTypeCount = 0> <#list restadmin("/users/id/${user.id?c}/roles").roles.role as role> <#assign userType += role.name > <#assign userTypeCount ++> <#-- only add separator if user has more than one role --> <#if (userTypeCount >1)> <#assign userType += ' | '> </#if> </#list>
I also tried to use the built in functionality of has_next and <sep> however this did not work either.
Does anyone have any suggestions?
It would resolve you problem
${userType?keep_before_last("|")}
I may create an issue if a user does not have any role but I think every user will have at least one role. Right?
Here is an updated version. Here is the output https://prnt.sc/jsccwr
<#assign userTypeCount = 0> <#assign userType = ""/> <#list restadmin("/users/id/${user.id}/roles").roles.role as role> <#assign userType += role.name > <#assign userTypeCount ++> <#-- only add separator if user has more than one role --> <#assign userType += ' | '> </#list> ${userType} ${userType?keep_before_last("|")}