File Template integer parameter/variable

I'd like to pass an integer variable to my file template, but it is interpreted as string:

#set( $inc = $a + 1 )

a  = $a

a + 1 = $inc

Create template with a = 2 and you get:

a = 1

a + 1 = 21

I expect (like to get):

a = 1

a + 1 = 3

How can I achieve this?


Comment actions Permalink

Variables passed in via the Template dialog are passed in as Strings. As a result, the $a + 1 is resulting in string concatenation and not addition. So you need to convert the String to an Integer.

The Template uses Apache Velocity Template Engine which in turn can use Java methods.  Ultimately, we want to use the Integer.paerseInt(String) method. From my understanding of Velocity, there is not a way in the template to call a static method without also adding some code to the Java side of things before parsing the template. Since we do not have access to the parser, we can use an alternate way of accessing the static method... we'll take advantage of the simple fact that instances of a class have access to that classes static method. We create a "dummy" 'Integer' variable to use to access the Integer classes static methods:

#set($Integer = 0)
#set($inc = $Integer.parseInt($a) + 1)

That should give you what you are after.

Comment actions Permalink

Thanks, great work around.


Please sign in to leave a comment.