Forum Discussion

Han's avatar
Han
Ace
7 years ago

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; <...
  • VikasB's avatar
    VikasB
    7 years ago

    Han

    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("|")}