March 08, 2004

Precisions on Canvas and Velocity

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...

Posted by cedric at March 8, 2004 10:37 AM
Comments

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.

Posted by: Erik Hatcher at March 8, 2004 11:09 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

Posted by: Bob Lee at March 8, 2004 11:31 AM

Ugh...

Workplaces are <%
workplaces["Cedric"] + " " + workplaces["Alois"]
%>

Posted by: Bob Lee at March 8, 2004 11:32 AM

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.

Posted by: Sam Pullara at March 8, 2004 02:40 PM

Thanks for Canvas. Any ideas how to use this with WebWork2?
Canvas would be nice in SnipSnap, the templates stored in snips.

Posted by: Stephan Schmidt at March 9, 2004 04:37 AM

The Canvas story continues...

http://wiki.codehaus.org/groovy/TemplateServlet

:)

Posted by: Christian Stein at March 31, 2004 10:44 PM

Lê Xuân Đồng đă ghé thăm nơi đây

Posted by: at June 1, 2004 11:53 AM

Các bạn đang __thảo luận__ ǵ

Posted by: at June 1, 2004 11:54 AM
Post a comment






Remember personal info?