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;

 <#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?

  • 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("|")}
  • Can you share the error screenshot. Also, try it with simple assignment instead += 

    <#assign userType = userType + ' | '>

     

    • Han's avatar
      Han
      Ace

      VikasB I am not getting any errors. If I remove the += then I overwrite my variable with a pipe, which is not my intended out come. 

      I would like to have the user roles displayed with a | separator if it's needed. 

      • Han

        You need to initialize the "userType" initially. Like this  

        <#assign userType = ""/>

        Here is the corrected version. 

        Note: It would add pipe after every role except in between of first and second role. 

         <#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 -->
            <#if (userTypeCount >1)>
              <#assign userType += ' | '>
             
            </#if>
          </#list>
        ${userType}

         You need to remove the <#if> condition If you want to add a pipe after the first role also.