Petteri Hietavirta Petteri Hietavirta - 7 days ago 5
Java Question

Velocity and ignoring java method return

I want to add an element to the list in my Velocity macro. Is this only way to avoid 'true' text appearing to the Velocity output?

#set($path = [])
#set($swallow = "a")

#set ($swallow = $path.add("blaablaa"))

Answer

I have to agree with Terence Parr (father of StringTemplate) who has relevant comments in an Artima interview and a good paper on model-view separation.

Templates should be restricted to pure presentation. Just as we shouldn't put presentation-layer details in the model, we shouldn't put data manipulation in our templates. Doing either usually ends up making maintenance more difficult, and even makes it harder to know where to do the maintainance.

Based on my own experience with both Velocity and StringTemplate, I'd strongly suggest taking the problem in your question (artifacts of data manipulation making the template more complicated) as a hint and moving all manipulation of data structures into the Java layer.