Freemarker Tips

I like FreeMarker. It's simple, functional and pretty fast tool.
In project we are using Freemaker with SpringMVC.

To work with message bundle are used few macros, one of them is spring.messageArgs. So today I've spent some time on searching how to pass argument to it. There was only one argument and, obviously, I was expecting that this macros could be used in next way:
<@spring.messageArgs "some.message.key" "argumentValue"/>

Unfortunately it's not so easy :(. The macros is calling method that accepts arguments as an array of objects. And string "argumentValue" isn't an array. So I found next simple solution:
<#assign args = ["Ruslan"]/>
<@spring.messageArgs "" args/>

where = Hello {0}!

Another interesting thing about Freemarker is the way you can print numbers. If you want to print number value in argument X as a regular number rather than formatted number, you may do it in next way ${X?c}. I always forget to add ?c suffix and later receive many errors. Fortunately, it's possible to use #{X} that has the same effect. I found that it saves my time.


Anonymous said...

What's the benefits of FM over Velocity ?

Ruslan K said...

Sorry for such delay. I was a bit busy last few weeks

Never was working with Velocity as primary template engine. But when I was choosing between Velocity and FreeMarker, the last one looked more functional. As I see, new versions of Velocity and FreeMarker are released since. So I've checked and now Velocity is as functional as FreeMarker.

Still I didn't find how to use JSP tag libs in Velocity, while that is possible in FreeMarker and we need it to work with Tiles. FreeMarker macro engine looks more functional and, IMHO, convenient.