Ad

Concat Strings With Separator In Velocity Template

- 1 answer

In my velocity template, I want to concat strings delimited by comma(,) using foreach loop.

#foreach($field in $MyObject.Fields)
    ${field.Name},
#end

This results in an extra comma at the end. How can avoid the last unwanted comma?

PS: I am using NVelocity 0.4.2

Thanks

Ad

Answer

I can't recall where NVelocity 0.4.2 comes from (i.e. Castle Project or the original port), but if it has the foreach looping directives, you can use them. Take a look at this recently asked question on the same topic:

NVelocity advance lopping syntax

If it doesn't have support for that, you can use the special $velocityCount variable with an #if directive to determine if you want a comma or not.

Ad
source: stackoverflow.com
Ad