How can I trim whitespace by Velocity

Surasin Tancharoen picture Surasin Tancharoen · Dec 21, 2011 · Viewed 31.2k times · Source

I have a method called render_something which can creates a lot of whitespace, for example:

<a href="#">#render_something('xxx')</a>

The result can be:

<a href="#">     

           something that generate from redner_something


              </a> 

Which actually I want it to be like this:

<a href="#">something that generate from redner_something</a>

Does velocity has something like this?

#trim(#render_something('xxx'))

Answer

Edd picture Edd · Dec 23, 2011

I just read this article on Velocity Whitespace Gobbling which suggests a few work-arounds including Velocity Whitespace Truncated By Line Comment.

This basically suggests commenting out line breaks by putting comments at the end of each line. It also suggests not indenting the code in your macros to prevent superfluous (one of my favourite words) spaces occurring.

TBH it's not a great solution but may suit your needs. Simply put ## at the end of each line in your macro and that will make things a little bit nicer... sort of