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
回答2:
It seems just java native trim() works.
$someValue.trim() works for me
回答3:
Solution
In the class where you create the VelocityEngine, add a method as follows
Although the behavior of Velocity is clearly define, it can be a bit tricky to see how it works sometimes. The separate trim()-method is necessary to get the char-sequence from the template into a Java method where you can call the actual trim() on the String. As far as I know there is no trim inside Velocity, but you always can call back to Java with tricks like this one.
The double-quotes are necessary because the #render_something is just a macro, not a function call, this means the results of the statements in the macro are put verbatim into the point where the macro is "executed".
The StructuredGlobbingResourceLoader we can find on the website has a complex behaviour and doesn’t get rid of any kind of whitespace, so I modified it to get the simple behaviour: "Delete any whitespace at the beginning of the lines, and add a comment at the end of each line" (which prevents the linebreak evaluation). The filter is applied on the input stream at loading time.
This kind of velocity template
#if($value) the value is $value #end
is transformed to
#if($value)## the value is $value###end##
Then if you want to have linebreaks or beginning of line whitespaces, you'll have to put($br,"\n") and put($sp," ") in your context like Vadzim's explained and explicitly use them in your template. This way of doing will allow you to keep indented templates, with maximum control.
take the class from this page http://wiki.apache.org/velocity/StructuredGlobbingResourceLoader change the extended class to the kind of loader your need (this one uses the webapp loader) replace the read() method with the code I provide use the class as your resource loader in your properties. Example for the webapp loader: webapp.resource.loader.class=...StructuredGlobbingResourceLoader
publicint read()throwsIOException{int ch;switch(state){case bol://beginning of line, read until non-indentation characterwhile(true){ ch =in.read();if(ch!=(int)' '&& ch!=(int)'\t'){ state =State.content;return processChar(ch);}}case content: ch =in.read();return processChar(ch);//eol states replace all "\n" by "##\n"case eol1: state =State.eol2;return(int)'#';case eol2: state =State.bol;return(int)'\n';case eof:return-1;}return-1;}//Return the normal character if not end of file or \nprivateint processChar(int ch){switch(ch){case-1: state =State.eof;return-1;case(int)'\n': state =State.eol1;return(int)'#';default:return ch;}}
In some cases, I've had to essentially minimize my script like I would js or css. It works well, though it is not as easy for humans to read. Just one other option to eliminate the excess space: