velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: Question on using RenderTool in Java
Date Thu, 10 May 2007 06:04:44 GMT
On 5/9/07, Matthew Twomey <mtwomey@beakstar.com> wrote:
> Greetings,

hi!
>
> I'm new to Velocity and I fear I may be misinterpreting the intent of this
> the "tools" package. What I'd like to do is use the RenderTool.eval function
> to process strings with template information in them.

The RenderTool is primarily for doing eval() stuff *within templates*
as opposed to within java.  This where most tools are designed to be
used, though they can be used elsewhere, of course.

> As a simple test of
> this (which isn't working) I am trying the following:
>
>
>
> // relevant imports
>
> import org.apache.velocity.VelocityContext;
>
> import org.apache.velocity.context.Context;
>
> import org.apache.velocity.tools.generic.RenderTool;
>
> .
>
> .
>
> .
>
> // relevant code
>
>             Context ctx = new VelocityContext();
>
>             RenderTool rtool = new RenderTool();
>
>             ctx.put("cat", "Beakman");
>
>             String s99 = rtool.eval(ctx, "Hello $cat");
>
> //
>
>
>
> What I'm hoping for here is a result where s99 contains "Hello Beakman".
> What happens is rtool.eval returns null.

this almost certainly means that the Velocity singleton failed to
initialize.  here is where using the RenderTool is probably not ideal
for your application.  We don't like tools to throw exceptions when
used in the middle of processing a template, so we catch them, log
them (when possible) and return null.  Within Java, you usually want
the exceptions rather than the gentle failure.  So, i'd recommend you
do something like this instead:

VelocityEngine engine = new VelocityEngine();
engine.init(); //configuration could optionally be done before this
VelocityContext ctx = new VelocityContext();
ctx.put("cat", "Beakman");
StringWriter out = new StringWriter();
engine.evaluate(ctx, out, "test template", "Hello $cat");
String s99 = out.toString();

it's a wee bit more code, but it won't swallow exceptions!

> I realize this type of simple text replacement is easy to do without
> Velocity - this is just my "hello world" test. Ultimately I am aiming to
> build complex router configurations that will require looping through many
> lines of access-lists applying various variables and so forth.

be sure you don't initialize a new VelocityEngine() for each line you
evaluate.  you should be able to create one at the start and use it
for all of them.

>
>
> I'd just like to be able to evaluate strings from within Java using the
> templating language (e.g. this isn't web or Tomcat related at all).
>
>
>
> Can anyone tell me what I'm doing wrong here, or if I'm going about it
> completely wrong - what I should be looking at?

if you really want to get your initial example working, you'll need to
find out where Velocity sent it's log messages.  here's some questions
that might help clarify that:  what versions of Velocity and
VelocityTools are you using?  did you configure the singleton prior to
using the RenderTool?  are you using the velocity-dep jar or the plain
vanilla velocity jar?

>
>
> Thanks,
>
>
>
> -Matt
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Mime
View raw message