org.apache.velocity.tools.view.tools
Class ViewRenderTool

java.lang.Object
  extended by org.apache.velocity.tools.generic.RenderTool
      extended by org.apache.velocity.tools.view.tools.ViewRenderTool

public class ViewRenderTool
extends RenderTool

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

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

      Output
      ------
      3

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

      Output
      ------
      hello world!


 Toolbox configuration:
 <tool>
   <key>render</key>
   <scope>request</scope>
   <class>org.apache.velocity.tools.view.tools.ViewRenderTool</class>
   <parameter name="parse.depth" value="10"/>
 </tool>
 

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 NOT meant to be used in either application or session scopes of a servlet environment.

Version:
$Revision: 479724 $ $Date: 2006-11-27 10:49:37 -0800 (Mon, 27 Nov 2006) $
Author:
Nathan Bubna

Field Summary
private  org.apache.velocity.context.Context context
           
 
Fields inherited from class org.apache.velocity.tools.generic.RenderTool
DEFAULT_PARSE_DEPTH, KEY_CATCH_EXCEPTIONS, KEY_PARSE_DEPTH
 
Constructor Summary
ViewRenderTool()
          Constructs a new instance
 
Method Summary
 java.lang.String eval(java.lang.String vtl)
          Evaluates a String containing VTL using the current context, and returns the result as a String.
 void init(java.lang.Object obj)
          Initializes this instance.
 java.lang.String recurse(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 org.apache.velocity.tools.generic.RenderTool
configure, eval, getCatchExceptions, getParseDepth, internalRecurse, recurse, setCatchExceptions, setParseDepth, setVelocityEngine
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

private org.apache.velocity.context.Context context
Constructor Detail

ViewRenderTool

public ViewRenderTool()
Constructs a new instance

Method Detail

init

public void init(java.lang.Object obj)
Initializes this instance.

Parameters:
obj - the current Context

eval

public java.lang.String eval(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:
vtl - the code to be evaluated
Returns:
the evaluated code as a String
Throws:
java.lang.Exception

recurse

public java.lang.String recurse(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.

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


Copyright (c) 2003-2007 Apache Software Foundation