How can I easily create a comma separated list if items?

I first ran into this problem while using Velocity to create SQL statements. The comma separated list of Table Columns can not have a trailing comma.

To VM_global_library.vm add the commaIfNeeded macro:

##-----------------------------------------------------------------
## Add commas in a comma separated list to all items but the last
## Param: $index - pass in $velocityCount
## Param: $list  - pass in your velocity list variable
##-----------------------------------------------------------------
#macro(commaIfNeeded $index $list)
#set ( $lastone = $list.Count - 1 )
#if( $index <= $lastone ),
#else

#end
#end

Use the commaIfNeeded Macro to create a comma separated list:

#foreach ($column in ${Table.ColumnList})
    p_${column.Name} IN "${Table.Name}"."${column.Name}"%Type#commaIfNeeded( $velocityCount  $Table.ColumnList )
#end
  • No labels