Hi,

the whitespace issue has been debated quite a lot, and we have a
consensus in this list that we will implement in the future a
gobble-none and a gobble-structured form; latter, with your
example:

## example of all whitespace being gobbled
#if ($user.isAuthorized)
   #text(Hello)
#else
   #text(Good Bye)
#end

## example of whitespace preservation:
   $footer
   #keepIndent()#escapeHTML($address)

If anyone has a comment on this, please add it to the wiki:
  http://wiki.apache.org/jakarta-velocity/VelocityWhitespaceGobbling


My 2cents for the velocity syntax:
- ${foo}$bar is common practice in shell scripts
- $foo.bar.baz() is the Java flavour for object properties
- and #if...#else...#end is common practice in C-preprocessors

:) Christoph


Jonathan Revusky wrote:
[snip]
The problem isn't that you don't have strict whitespace control. You do. even in Velocity probably. (Though you have to be willing to hold your nose.)

The real problem is that you want to have strict whitespace control and have your templates be human-readable. In the following fragment:

#if ($user.isAuthorized)
   Hello
#else
   Good Bye
#end

there is a lot of whitespace that exists solely in order to make the template more human-readable: certainly, the line-break after the #if directive, the line break after the #else instruction, for example. They are almost certainly there solely for template readability.

Also, it is *highly likely* that the offsets before "Hello" and "Good Bye" as well as the line breaks following those strings were placed there solely to make the raw template more readable.

In the general case, how can a template engine deduce which whitespace is there for template readability (and thus should be ignored) and which whitespace really belongs in the output?

It is actually a very difficult problem. In fact, at times, I have thought it to be intractable. [snip] The traditional workaround has been to write something like:

#if $(user.isAuthorized)Hello#else#**#Good Bye#end

Of course, it occurs to me that you could maybe use the above comment hack more liberally in order to be able to write the fragment on multiple lines:

#if $(user.isAuthorized)#*
   *#Hello#*
*##else#*
   *#Good Bye#**
**##end

that's what I'm doing sometimes now, and it's yuck to mainatin!


But is this reasonable?

Again, the fact that it is possible to achieve precise control of whitespace is not the key point. It is that you want such control while having a reasonable, human-readable template. That is the raison d'ĂȘtre of templates really.

Okay, I guess no Revusky posting would be complete without a FreeMarker plug. The FreeMarker solution was to introduce whitespace trimming directives that are applied at parse-time.

So, in FreeMarker, you could write:

<#if user.isAuthorized>
    Hello<#t>
<#else>
    Good Bye<#t>
</#if>

That is exactly in the line of:
  
http://wiki.apache.org/jakarta-velocity/VelocityWhitespaceGobbleStructuredTemplates

The <#t> directives indicate that the opening and trailing whitespace on the line is to be gobbled, i.e. it is there to make the template human-readable. (There are also whitespace gobbling rules that say that the if else and the closing tag, since they occur solely on a line with no other whitespace output, gobble their opening and closing whitespace.)
[snip]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to