Reputation: 2671
I'm using velocity templates to generated plain-text emails for a notification system. I have a list of "Addressee" POJOs which contain a user ID and email address. I want to iterate over these POJOs and output the user id in one line (and eventually separated by some delimiter such as commas). I'm having problems formatting the output. For instance:
For Info: #foreach( $addressee in $info_addressees )$addressee.userId #end
For Action: #foreach( $addressee in $action_addressees )$addressee.userId #end
Unfortunately this does not create a new line after each foreach loop. So i get something like:
For Info: user1 user2 user3 For Action: user4 user 5 user6
The only way I seem to be able to add a new line at the end of each foreach loop is by adding some additional character(s), such as:
For Info: #foreach( $addressee in $info_addressees )$addressee.userId END #end
For Action: #foreach( $addressee in $action_addressees )$addressee.userId END #end
I tried using whitespace at the end but it seems that these are gobbled and hence ignored (not what I expected).
What's the right way to do this? And could it be done with a macro?
The output from templating must be plaintext (it cannot be structured like HTML); and, the formatting must be done in the template and not in before being put into the template model.
Upvotes: 1
Views: 4560
Reputation: 6933
#set( $n = '
')
#foreach($foo in $bar)$foo $n#end
or use the EscapeTool from the VelocityTools project
#foreach($foo in $bar)$foo $esc.n#end
Upvotes: 2