velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <ge...@optonline.net>
Subject Re: Whitespace, redux
Date Mon, 15 Apr 2002 12:33:51 GMT
On 4/13/02 5:35 AM, "Joaquim Carvalho" <jrc@bookmarc.pt> wrote:

> 
> I have worked a long time ago in a templating language for web apps used
> in house for over 5 years.
> 
> We had a similar whitespace problem (control flow directives created
> zones on the templates that produced undesired white space on the
> output).
> 
> The solution we found was to add two whitespace related directives,
> "output" and "echo", that in Velocity would look something like this:
> 
> 1. "output" switches output on and off (this applies to ALL output from
> the template to the output stream)
> 
> #output("off") ## from now on there is no output from the template but
> the enclosed directives still are executed
> #if ($option1)
>   #set( $a = "hello")
> #else
>   #set($a = "goodbye")
> #end
> #output("on") ## restoring output
> $a
> 
> 
> 2.After a #output("off") text could be forced to the output stream by
> using #echo:
> 
> #output("off")
> #if ($option1)
>   #echo("hello")
> #else
>   #echo("goodbye")
> #end
> #output("on") ## restoring output
> 

This is *damn* cute. :)  Further, I would bet that this could be done
completely outside of the Velocity core.  No, not "yet another wet-blanket
attempt by Geir" :) but certainly we could evaluate this approach
independent of changes to the core.


> 3. In macros and #parsed templates it is necessary to save and restore
> the previous output status so that other templates can safely use code
> in other files:
> 
> 
> $macro ( testOption, $option1)
> 
> #output("hold") ## Sames as "off" but the current output state is
> remembered
> #if ($option1)
>   #set( $a = "hello")
> #else
>   #set($a = "goodbye")
> #end
> #output("restore") ## restoring output to previous status
> #end
> 
> 

Good - I was thinking that you might always want to restore previous state,
but I guess the downside is that you could get into trouble by forgetting.
Still...

> 
> this has dealt with our whitespace handling problems for good. When
> writing sequences of directives, specially involving control structures
> that need to be indented for readability we just backet that part of the
> template with #output("off") #output("on") with an ocasional #echo
> inside if something needs to go out the stream.

And with #echo("...") you would get full Velocity interpolation in the
output :

  #echo("Hello $foo : #woogie($bar)")


> 
> This also is nice for Velocity usage just as  aruntime scripting
> language not related to any type of outputing. We are using Velocity
> right now in such a setting: a bibliographic application needs to do
> some manipulation of library records according to transformation rules
> that are defined at runtime. By adding specially designed objects to the
> context we can use velocity as the rule language and templates just
> specify how an object is transformed into another one. The output from
> the template is irrelevant, so we could just as well avoida its
> production.
> 
> As far as velocity goes I think such a solution would be backwards
> compatible; an implicit #output("on") is assumed for evey template (but
> not for the parsed ones).
> 
> I think even if a template using #output and #echo would find itself
> being rendered by an older version of Velocity it would still work, I
> think, because #output and #echo would just be ignored (although of
> course the ouput would not be quite the desired one).
> 
> This is not a request for modification, just a contribution to the
> discussion.

And a good one.  Thanks!

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"The greatest pleasure in life is doing what people say you cannot do."
        - Walter Bagehot



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


Mime
View raw message