Search code examples
velocity

Added comma separated on the foreach Velocity loop


What should I added to the following foreach loop (Velocity codes) to get the final result like appNames=A,B,C

   #if($approval.has()) 
   #foreach($item in $approval.rejected)
   #set($appNames =$item.appName)
   #end
   #end

Thanks


Solution

  • Just simply do add separator to it in the loop and String concatenation

    #if($approval.has()) 
    #set($appNames ="")
    #set($separator="")
    #foreach($item in $approval.rejected)
    #set($appNames  =$appNames  +$separator +$item.appName)
    #set($separator = ",")
    #end
    #end
    

    appNames= $appNames

    output

    appNames= A,B,C