velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Smith, Mike H" <Mike.H.Sm...@pfizer.com>
Subject RE: Overriding the default precedence of the template engine
Date Fri, 21 Jul 2006 12:19:29 GMT
Thanks to all responders on this.

As inferred, my preference is to remove the burden of handling
precedence + defaults from the template author.

For now I've implemented a simple workaround, by mandating that all
#sets are of a regular format, and can thus be parsed, removed and then
applied to the context along with any "overrides".

For our simple requirements this is workable, but I can see that #4
below would be the more elegant solution.

Thanks again.
Mike

-----Original Message-----
From: apache@recks.org [mailto:apache@recks.org] 
Sent: 21 July 2006 09:31
To: Velocity Users List
Subject: Re: Overriding the default precedence of the template engine

There are several ways to solve this.

1. With the simple #if contstruct as presented in this
    thread:
#if (! $this)
  #set ($this = 'Velocity')
#end
$this is great!

2. With a macro:
#macro( valueOrDefault $value $default )##
#set( $localValue = $value )## this is accesses $value only once!
#if(!$localValue)$localValue#else$default#end##
#end
...
#valueOrDefault($this, "Velocity") is great!

3. Do the same as the macro with a tool - there was one either
    in the contributions area (or in Turbine) with an isNull()
    allowing a default:
$tool.valueOrDefault($this, "Velocity") is great!

4. The most powerful and implicit solution is to create
    your own context implementation, that takes the value from
    the background ChainedContext if it is defined.
    Look at the implemetnation of the AbstractContext:
http://jakarta.apache.org/velocity/docs/api/org/apache/velocity/context/
AbstractContext.html
    -> Creating a read-only context, it would do exaclty
    what you expect.

Cheers,
Christoph

Smith, Mike H wrote:
> Hi,
> 
>  
> 
> Is there a way to modify the precedence logic of the template engine?
> 
>  
> 
> In particular, I'd like to:
> 
>  
> 
>  
> 
> 1.	Define a template like:
> 
>  
> 
> #set( $this = "Velocity")
> 
> $this is great!
> 
>  
> 
> 2.	Within the application code, set the "this" property to be
> something different (e.g. "Laughter")
> 3.	Then have the engine render as "Laughter is great"
> 
>  
> 
>  
> 
> With the current implementation the rendering precedence is template
> then context, any easy way to reverse this?
> 
>  
> 
> Thanks.
> 
> Mike
> 
>  
> 
>  
> 
>  
> 
> 

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


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


Mime
View raw message