John, I'm answering your question back on the list so other folks can see it.
[EMAIL PROTECTED] wrote:
> Sorry Christopher, quick question. How do you reference the velocity
> tool? Like to you put some import in the .vm file? This apache
> velocity stuff is all new to me.
That depends on how you are using Velocity, because there are super
simple ways to do it, and there are slightly more involved.
If you are using Velocity to, say, generate an email message or some
other use that doesn't involve taking a request for a .vm file and
automatically runing it through Velocity, then you need to do something
like this already:
Velocity velocity = ...;
Template template = ...;
VelocityContext context = ...;
OutputStream output = ...;
template.merge(context, output);
The only thing you have to do it to put an instance of the tool in
question into the context before merging the template, like this:
context.put("NumberTool", new
org.apache.velocity.tools.generic.NumberTool());
Then, in your template, just use it like any other object reference:
"123" as an integer looks like $NumberTool.integer("123").
If you are using VelocityViewServlet or VelocityLayoutServlet (which are
part of the velocity-tools package IIRC), then you should be able to set
up a "toolbox" that will be loaded automatically as appropriate into the
velocity context.
See the documentation for velocity-tools "view" here:
http://velocity.apache.org/tools/releases/1.4/view/
Just search for uses of the word "toolbox". Many tools (including
NumberTool) don't need to be created separately for every template or
every run-through of a template: you can have Velocity*Servlet create
one object and use it for all of your velocity contexts.
-chris
signature.asc
Description: OpenPGP digital signature
