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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top