I have to say I am surprised by some of the comments I have received about
Canvas, and in particular from people who say that the Velocity Template
Language (VTL) is a better choice than Groovy because it’s not a "real"
language, so you are not tempted to mix presentation and business logic.
First of all, the assumption that the generated template is for
"presentation" (probably meaning: a Web page) is a generalization.
Velocity is used for much more than just generating Web pages, so this argument
is weak in my opinion.
But if you think the VTL is just a declarative language, you are fooling
yourself. VTL has
- Control structures (if, while, etc…).
- Variables.
- Macros.
- And worst (or best) of all, it has access to the underlying Java
objects.
In other words, I am pretty sure that you can use VTL to write arbitrarily
complex pieces of code inside your template, and
Sam was quick to point out a
perfect example of that.
With that in mind, if we are going to have to deal with a language inside the
template, I would rather use one that I like, is powerful and ideally, close to
the Java syntax.
There is also another reason why VTL is less than optimal for this, and it
has to do with the philosophy behind Velocity templates.
Velocity templates make no distinction between "verbatim text" and VTL.
The only way to identify the pieces of VTL code in your template is by spotting
the various syntactic annotations (#, $, etc…). The problem with this
approach is that it makes it pretty much impossible to guarantee the indentation
of the generated files, since you can never tell when a space is significant
(part or your "verbatim text") or should be ignored (used to indent a VTL
expression).
The JSP syntax (or any delimiter-based syntax) makes it obvious what spaces
should be preserved and which ones can be ignored for the generation of the
final file:
// Canvas template
public class GeneratedClass {
<%
for (i in methods) { i.generate() }
%>
}
From my experience, it is absolutely impossible to get a decent indentation
for this very simple example with Velocity…
#1 by Erik Hatcher on March 8, 2004 - 11:09 am
So nice to have java.net trackbacks!
I’m not going to defend Velocity versus Canvas, even as the author of the article you refer to. Yes, it is somewhat unfortunate that Velocity has access to the underlying objects, but certainly this can be dealt with by only placing dumb objects into the context.
We use Velocity as a simple way to allow clients to customize e-mail templates and snippets which get injected into the presentation (legalese, instructions, and such). Velocity syntax is much friendlier to non-technical users than JSP-like syntax such as Canvas, IMHO.
That being said – Canvas is quite cool, as is Groovy. I certainly will consider integrating them into my applications in the future. Canvas would make a very nice substitute for our Ruby code generation using ERB.
Another project to compare to, on the extreme end of MVC templating engines is Parr’s StringTemplate engine.
#2 by Bob Lee on March 8, 2004 - 11:31 am
You should add BeanShell support as well. The reason I prefer it sometimes is not so much for my sake, but for other developers’. Anything you can do in Java will work in BeanShell. Plus, it has some nice syntactic sugar of its own. The last example from the Canvas home page in bsh:
Workplaces are
#3 by Bob Lee on March 8, 2004 - 11:32 am
Ugh…
Workplaces are <%
workplaces[“Cedric”] + ” ” + workplaces[“Alois”]
%>
#4 by Sam Pullara on March 8, 2004 - 2:40 pm
Cedric’s so smart and had such a good idea that I checked in Template/TemplateEngine interfaces into the groovy.text package along with a SimpleTemplateEngine that uses JSP scriptlet and EL syntax. Some trivial examples are in the TemplateTest unit test. You can get these features if you build from CVS. Using these interfaces you can build other template engines that do their transformations however you like without changing the basic way you access them.
#5 by Stephan Schmidt on March 9, 2004 - 4:37 am
Thanks for Canvas. Any ideas how to use this with WebWork2?
Canvas would be nice in SnipSnap, the templates stored in snips.
#6 by maps and legends - craig pfeifer on March 14, 2004 - 11:38 am
Velocity Abandoned?
According to TSS, the Velocity project has been abandoned? After the tongue lashing I received from Jon Scott Stevens in response to my book review I can’t say I’ll miss it terribly. Cedric thinks Velocity has certain problems as well….
#7 by Christian Stein on March 31, 2004 - 10:44 pm
The Canvas story continues…
http://wiki.codehaus.org/groovy/TemplateServlet
🙂
#8 by Anonymous on June 1, 2004 - 11:53 am
L
#9 by Anonymous on June 1, 2004 - 11:54 am
C