Re: Extra whitespaces surrounding tokens

SanfordWhiteman
Level 10 - Community Moderator

Re: Extra whitespaces surrounding tokens

That's right, Velocity is strictly space-preserving, as I mentioned above:

if you have whitespace, it's preserved by default, because you might be outputting into plain text (not just plain-text email but any old text file) where CRs are critical for proper formatting.

So there isn't actually such a thing as an "unused" line -- a trailing CR is as important as any other CR.

If you want whitespace in your code for readability, it can help to use #define blocks instead of direct output.

For example:

#define( $output )



This

has

line

breaks

before

and

after



#end
${output.toString().trim()}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The, er, defining of the #define doesn't output anything. Output is managed by String-ifying the #define and then trim()-ing it first. So the final email wouldn't have the before and after linebreaks.

See also https://nation.marketo.com/community/product_and_support/blog/2018/09/22/in-velocity-tokens-trim-you...