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 Fri, 11 May 2007 16:29:57 GMT
Great!  Thanks for letting us know how it's going!

On 5/10/07, Matthew Twomey <mtwomey@beakstar.com> wrote:
> Nathan,
>
> Thank you very much - that was just what I needed to get going. I am now up
> and running (at least with some rudimentary test templates).
>
> I was initially using the velocity-tools package and including
> "velocity-tools-1.3.jar" as well as "velocity-1.4.jar" in my classpath. I
> had some trouble getting the resourceloader and logging going and I've since
> switched to the full velocity 1.5 system. I'm including "velocity-1.5.jar"
> as well as everything in the lib subdirectory.
>
> This appears to be working perfectly at this point, so I'm on to building my
> template. Thanks again for your help!
>
> -Matt
>
> -----Original Message-----
> From: Nathan Bubna [mailto:nbubna@gmail.com]
> Sent: Thursday, May 10, 2007 1:05 AM
> To: Velocity Users List
> Subject: Re: Question on using RenderTool in Java
>
> 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
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

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


Mime
View raw message