You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2011/10/31 15:29:02 UTC

Avoiding whitespace

All,

The User Guide says that "Velocity's behavior is to gobble-up excess
whitespace" and offers this example:

Velocity's behaviour is to gobble up excess whitespace. The preceding
directive can be written as:

"
Send me
#set( $foo = ["$10 and ","a pie"] )
#foreach( $a in $foo )
$a
#end
please.

or as

Send me
#set($foo       = ["$10 and ","a pie"])
                 #foreach           ($a in $foo )$a
         #end please.

In each case the output will be the same.
"

I'm not observing this behavior. In my templates, the trailing
whitespace after a directive on a line (usually just a newline) appears
to be trimmed, but leading whitespace is preserved. So, the output from
the second example looks like this:

Send me
                 $10 and
         a pie
          please.

(note the leading whitespace before "$10 and", etc. which you may not be
able to see if you are viewing this message in HTML mode).

Am I missing some kind of configuration parameter? I don't appear to
have anything in my configuration that has anything to do with whitespace.

Is the documentation wrong? Or am I just mis-reading it?

I know the issue of whitespace comes-up a lot, and I'm sorry to add to
the never-ending discussion.

Thanks,
-chris


Re: Avoiding whitespace

Posted by Nathan Bubna <nb...@gmail.com>.
Gobbling mostly happens at the parser level right now.  This means
it's not extensible or configurable.  All you can do is modify the
parser and custom build Engine, modify the input (smart resource
loader), or modify the output (jTidy and friends).

http://wiki.apache.org/velocity/VelocityWhitespaceGobbling
https://issues.apache.org/jira/browse/VELOCITY-253

In my ideal world, the parser would treat the whitespace
preceding/following directives as a node type themselves or perhaps as
a distinct part of the directive nodes, allowing us full freedom to
configure whitespace behavior around directives.  (

In my optimistic world, i'd be perfectly happy for Engine 2.0 to
simply have a modified parser that gobbles more  predictably (i.e.
according to Christoph's very nice rules).

In my practical world, it would even be an improvement to just change
the parser to gobble nothing and leave whitespace management fully to
resource loaders or post-processors like jTidy.

But in reality, no one is doing any of this work, including myself.
Velocity is at the dreaded "good enough" point where no flaw is itchy
enough to inspire much hacking.  :(  But you could be the one! :)


On Wed, Nov 2, 2011 at 12:11 PM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> Nathan,
>
> On 10/31/11 11:45 AM, Nathan Bubna wrote:
>> In that case, the documentation is wrong.  Leading whitespace is only
>> trimmed from in front of a #set directive that has nothing after it.
>> I'm not aware of any other case where leading whitespace is trimmed.
>
> Is this something that could be extended to directives other than #set?
> I just modified some of my XML-emitting templates to adjust whitespace
> and, especially in loops, tons of extra whitespace is generated. I ended
> up inserting VTL comments to get the job done and still have my
> directives indented properly (or I'd lose my sanity).
>
> Thanks,
> -chris
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Avoiding whitespace

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Nathan,

On 10/31/11 11:45 AM, Nathan Bubna wrote:
> In that case, the documentation is wrong.  Leading whitespace is only
> trimmed from in front of a #set directive that has nothing after it.
> I'm not aware of any other case where leading whitespace is trimmed.

Is this something that could be extended to directives other than #set?
I just modified some of my XML-emitting templates to adjust whitespace
and, especially in loops, tons of extra whitespace is generated. I ended
up inserting VTL comments to get the job done and still have my
directives indented properly (or I'd lose my sanity).

Thanks,
-chris


Re: Avoiding whitespace

Posted by Nathan Bubna <nb...@gmail.com>.
In that case, the documentation is wrong.  Leading whitespace is only
trimmed from in front of a #set directive that has nothing after it.
I'm not aware of any other case where leading whitespace is trimmed.

On Mon, Oct 31, 2011 at 7:29 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> All,
>
> The User Guide says that "Velocity's behavior is to gobble-up excess
> whitespace" and offers this example:
>
> Velocity's behaviour is to gobble up excess whitespace. The preceding
> directive can be written as:
>
> "
> Send me
> #set( $foo = ["$10 and ","a pie"] )
> #foreach( $a in $foo )
> $a
> #end
> please.
>
> or as
>
> Send me
> #set($foo       = ["$10 and ","a pie"])
>                 #foreach           ($a in $foo )$a
>         #end please.
>
> In each case the output will be the same.
> "
>
> I'm not observing this behavior. In my templates, the trailing
> whitespace after a directive on a line (usually just a newline) appears
> to be trimmed, but leading whitespace is preserved. So, the output from
> the second example looks like this:
>
> Send me
>                 $10 and
>         a pie
>          please.
>
> (note the leading whitespace before "$10 and", etc. which you may not be
> able to see if you are viewing this message in HTML mode).
>
> Am I missing some kind of configuration parameter? I don't appear to
> have anything in my configuration that has anything to do with whitespace.
>
> Is the documentation wrong? Or am I just mis-reading it?
>
> I know the issue of whitespace comes-up a lot, and I'm sorry to add to
> the never-ending discussion.
>
> Thanks,
> -chris
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org