org.apache.velocity.tools.view
Class ViewRenderTool
java.lang.Object
org.apache.velocity.tools.generic.AbstractLockConfig
org.apache.velocity.tools.generic.RenderTool
org.apache.velocity.tools.view.ViewRenderTool
@InvalidScope(value={"application","session"})
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:
<tools>
<toolbox scope="request">
<tool class="org.apache.velocity.tools.view.ViewRenderTool">
<property name="parse.depth" type="number" value="10"/>
</tool>
</toolbox>
</tools>
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.
- Since:
- VelocityTools 2.0
- Version:
- $Revision: 591088 $ $Date: 2007-11-01 10:11:41 -0700 (Thu, 01 Nov 2007) $
- Author:
- Nathan Bubna
|
Field Summary |
private org.apache.velocity.context.Context |
context
|
|
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. |
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. |
void |
setVelocityContext(org.apache.velocity.context.Context context)
Sets the current Context. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
context
private org.apache.velocity.context.Context context
ViewRenderTool
public ViewRenderTool()
setVelocityContext
public void setVelocityContext(org.apache.velocity.context.Context context)
- Sets the current
Context. This is required
for this tool to operate and will throw a NullPointerException
if this is not set or is set to null.
- Overrides:
setVelocityContext in class RenderTool
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.
- Overrides:
eval in class RenderTool
- 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.
- Overrides:
recurse in class RenderTool
- Parameters:
vtl - the code to be evaluated
- Returns:
- the evaluated code as a String
- Throws:
java.lang.Exception- See Also:
RenderTool.eval(String)
Copyright (c) 2003-2007 Apache Software Foundation