But Velocity does not offer the softness Smarty has : imagine your web designer needs a date format tag in order to pretty print a date variable. In Smarty, you just need to write a plugin (ok, for dates it is just included ;-)), and it is straight available in your templates. With Velocity, you must imagine all usages of the variables or objects you expose before you make a template. I mean that you must think that a web designer would like to format a date, then expose the date utility to the template.
In this case, in order to be sure you won’t forget anything, you could simply expose all of your utility classes to the template, but this is fondamentaly unnecessary, ugly and you could possibly never imagine everything a template writer could need. The problem is right there : with Velocity, if you did not think of something at the design time, then it will not be possible to do at runtime, just because template variables and utility classes are exposed programmatically. It is just right for variables, but a problem for utility classes. That’s the main drawback I found to Velocity.
Then came Groovy and its SimpleTemplateEngine. Basically, it does the very same as Velocity for templating, but its main advantage is that you can embed groovy code into the template :
import groovy.text.SimpleTemplateEngine
def tpl_src ='''
<% def formatDate(date) {
new java.text.SimpleDateFormat("EEE, MMM d, ''yy").format(date)
}%>
The date today is <% print formatDate(date) %>'''
def binding = [ "date": Calendar.getInstance().getTime() ]
def engine = new SimpleTemplateEngine()
println engine.createTemplate(tpl_src).make(binding)
Just as simple as that ! You won’t need to rebuild an application in order to make an utility class, which has fundamentally nothing to do with the application logic, available to a template (front end logic). That’s where I liked Smarty so much, and makes me think I should actually think of replacing my Velocity templates with Groovy ones.