views:

182

answers:

2

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"))
+2  A: 

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.

joel.neely
+1  A: 

Yeah, if you want to shut it up, then #set is your friend. Many people shortcut this with a macro like this:

macro( call $this )#set( $stfu = $this )#end

Then you can abbreviate to

call( $path.add('blaablaa') )

Nathan Bubna