file under misc.

stuff which doesn't fit anywhere else 
« Back to blog

Using Variables in Velocity Method Parameter Lists

I just had to troubleshoot some problems with Velocity. The issue was related to a method in a template like this:

${messages.getMessage("thank.you", null, locale)}

In this case, the messages variable is a class implementing Spring's MessageSource interface and used for localization in the Velocity template (which is used for generating emails).

The problem was that the "locale" parameter was always null even though I was setting it in the Velocity model Map. A quick review of the Velocity User Guide and VTL Reference didn't yield any help.

I finally figured it out, though. The proper syntax for using a variable in the parameter list is:

${messages.getMessage("thank.you", null, $locale)}

You still need the dollar sign in front of the variable name. I had assumed the initial dollar sign created a variable-friendly scope to the text inside the brackets but I was wrong. My frustrations were compounded by the lack of examples on the Velocity site which used variables in parameter lists - they all have either static String parameters or no parameters at all. Meh.

Comments (0)

Leave a comment...