org.apache.velocity.tools.generic
Class RenderTool

java.lang.Object
  extended byorg.apache.velocity.tools.generic.RenderTool
Direct Known Subclasses:
ViewRenderTool

public class RenderTool
extends java.lang.Object

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

NOTE: These examples assume you have placed an instance of the current context within itself as 'ctx'. And, of course, the RenderTool is assumed to be available as 'render'.

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

      Output
      ------
      3
 
 Example of recurse():
      Input
      -----
      #macro( say_hi )hello world!#end
      #set( $foo = '#say_hi()' )
      #set( $bar = '$foo' )
      $render.recurse($ctx, $bar)

      Output
      ------
      hello world!

 

Ok, so these examples are really lame. But, it seems like someone out there is always asking how to do stuff like this and we always tell them to write a tool. Now we can just tell them to use this tool.

This tool is safe (and optimized) for use in the application scope of a servlet environment.

Version:
$Revision: 1.8.2.1 $ $Date: 2004/05/17 17:09:43 $
Author:
Nathan Bubna

Field Summary
private static java.lang.String LOG_TAG
           
 
Constructor Summary
RenderTool()
          Constructs a new instance
 
Method Summary
 java.lang.String eval(org.apache.velocity.context.Context ctx, java.lang.String vtl)
          Evaluates a String containing VTL using the current context, and returns the result as a String.
 java.lang.String recurse(org.apache.velocity.context.Context ctx, java.lang.String vtl)
          Recursively evaluates a String containing VTL using the current context, and returns the result as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG_TAG

private static final java.lang.String LOG_TAG
See Also:
Constant Field Values
Constructor Detail

RenderTool

public RenderTool()
Constructs a new instance

Method Detail

eval

public java.lang.String eval(org.apache.velocity.context.Context ctx,
                             java.lang.String vtl)
                      throws java.lang.Exception

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

Parameters:
ctx - the current Context
vtl - the code to be evaluated
Returns:
the evaluated code as a String
Throws:
java.lang.Exception

recurse

public java.lang.String recurse(org.apache.velocity.context.Context ctx,
                                java.lang.String vtl)
                         throws java.lang.Exception

Recursively evaluates a String containing VTL using the current 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.

FIXME? add a parse-depth to prevent infinite recursion?

Parameters:
ctx - the current Context
vtl - the code to be evaluated
Returns:
the evaluated code as a String
Throws:
java.lang.Exception


Copyright (c) 2003 Apache Software Foundation