Re: Functions

Mike Kienenberger <[email protected]> Thu, 19 Feb 2015 10:41:09 -0500
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <CAM1yOjZSns0pPpvKDxvjw_x=6J3zV9zj6B_38vTUcLi_Xt89Fg@mail.gmail.com>
Depends what you need.

Take a look at # macros if the only output from the function is generated text.

http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html#Velocimacros


Otherwise, the way to do this is to create a new tool (which is any
java class with a zero-arg constructor aka POJO) and add it to your
toolkit (covered in the other thread and a bit complicated in your
case).  But the tool itself is as trivial as it sounds.

class FooTool {
      public Integer barFunction(Object x, String y, List z) {
           // return Integer.valueOf(...);
      }
}

Assuming FooTool is registered under tool name "foo", then use this in
your template.

${foo.barFunction(x, y, z)}

Look at the existing velocity tools (like math) if you need more examples.




On Thu, Feb 19, 2015 at 10:22 AM,  <[email protected]> wrote:
>
> Is there a way to create dynamic methods/functions in Velocity Templates?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>