velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apa...@recks.org
Subject Re: Overriding the default precedence of the template engine
Date Fri, 21 Jul 2006 08:30:32 GMT
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


Mime
View raw message