The Velocity Project < Velocity Tools - Library >

Velocity Tools

Generic Tools

Tool Documentation

Other Subprojects

RenderTool Reference Documentation

This tool exposes methods to evaluate the given strings as VTL (Velocity Template Language) and automatically using the current context.

Class
 org.apache.velocity.tools.generic.RenderTool
Name
 $render (this is the recommended name of the tool in the Velocity context)
Toolbox Configuration Example
 
<tool>
  <key>render</key>
  <scope>application</scope>
  <class>org.apache.velocity.tools.generic.RenderTool</class>
</tool>
Author(s)
  Nathan Bubna
Method Overview
eval() Evaluates a String containing VTL using the current context, and returns the result as a String.
recurse() Recursively evaluates a String containing VTL using the current context, and returns the result as a String.

eval()

Evaluates a String containing VTL using the current context, and returns the result as a String.

String eval(Context ctx, String vtl)

Parameters
ctx
The Context to be evaluate the vtl with.
vtl
The code to be evaluated.
Returns
The evaluated code as a String.

Evaluates a String containing VTL using the given context, and returns the result as a String. If this fails, then null will be returned. This evaluation is not recursive.

#set( $list = [1,2,3] )
#set( $object = '$list' )
#set( $method = 'size()' )
$render.eval($ctx, "${object}.$method")

This will produce the following output:

3


recurse()

Recursively evaluates a String containing VTL using the current context, and returns the result as a String.

String recurse(Context ctx, String vtl)

Parameters
vtl
The code to be evaluated.
ctx
The Context to be evaluate the vtl with.
Returns
The evaluated code as a String.

Recursively evaluates a String containing VTL using the given context, and returns the result as a String. It will continue to re-evaluate the output of the last evaluation until an evaluation returns the same code that was fed into it.

#macro( say_hi )hello world!#end
#set( $foo = '#say_hi()' )
#set( $bar = '$foo' )
$render.recurse($ctx, '$bar')

This will produce the following output:

hello world!



Copyright © 1999-2003, Apache Software Foundation