Wednesday, March 21, 2007

[Pub] Apache Velocity

Java Template Engines

In the current Infoweek.ch magazine I discuss Java-based template engines. One of the earliest tools in this category was Webmacro. However, the problem at the time was, that Webmacro was published under Gnu Public License, which obviously makes it unusable for many (if not most) projects, even open source projects. So a team of developers started Apache Velocity. Meanwhile also a new project Freemarker offers interesting functionality in the open source Java domain.

Templates?
Templating is an alternative approach for code generation opposed to methods like JSP or XSLT. Generally spoken, templates define the fixed structure of the (textual) output and the parts that should be replaced during runtime. To give an example:



This is a template written in Velocity Template Language (VTL): First, "static" information, i.e., text is sent to output without change. Variables can be declared using the $variable syntax. These variables are exchanged at runtime with the desired values. The exchange of information between the Java class and the template engine happens using a context. In the Java class objects can be put into the context and in the template these objects can be accessed.

In the first simple examples: $firstname, this object is a String. The next example: $person illustrates a more complex example: Person is actually a Java class and in the template instance variables or methods can be accessed.

Finally simple logic can be performed, typically to iterate lists using the #foreach commands or checking conditions using #if.

The Java code to work with Velocity is fairly simple:



After initialisation, the Velocity context can be used to exchange data between application and template. Generally arbitrary objects can be put into the context. Calling the mergeTemplate method eventually renders the result.

Advantages of Template Engines and Application Scenarios
Template engines like Velocity can be used for various code-generation scenarios; typical examples are HTML, SQL and Java code generation. Thus Velocity is used in O/R mapping tools, UML tools for code generation and so on. A significant advantage of template-based HTML generation is, that cooperation of developer and designer is easier compared to XSLT-based approaches. However languages like XSLT are more powerful when the data and generated output is less structured.

Also the "learning curve" of tools like Velocity is very smooth, so results can be produced quickly. My suggestion is to get an overview on these technologies when a decision for code-generation tools is required in a project.

Webmacro, Velocity and Freemarker?
All three projects have a significant user community. My impression is, that the community is most active in Velocity and Freemarker. These two projects have a similar, but in details different approach that is worth evaluating. E.g., Freemarker apparently has better support for rendering XML data using templates, Velocity seems to have a larger user-base on the other hand.

For more details check out the infoweek.ch article (German).

7 comments:

Saifuddin said...

Thanks for the great post to providing the information on Apache Velocity. well, I also doing my Project on Code Generation. it would generate a source code for Java web application. creating DAO, beans, and Controller as well as JSP for presentation layers. but one thing which i haven't selected yet is Template Engine. Can you help to find out what would be the best and easy writing template for my Code Generation.

Thanks in Advance.

Alexander Schatten said...

This is really a difficult question without knowing more details. But in general Velocity is capable of doing so, as projects like Cayenne show.

Saifuddin said...

Thanks for reply Schatten,

your reply is more likely in the favor of Velocity, well if I choose Velocity how about the complexity of writing template for Client and Server. as i used Java for my Domain Language for creating My Generator.

Saifuddin said...

hello Alexander schatten,

waiting for your reply, MyCodeGenerator is like accept a Model(as database schema) and generates DAOs,Bean, Controller with the help of wizards and finally generating a interfaces by using JSP,HTML. this is all about my myCode Generator. as I interested in template-driven approach to transforming my model into source code.

I hope this details is enough to understand my system,

Looking forward for you reply..

Alexander Schatten said...

I am sorry, but this is not a support forum for Velocity. Evaluation of the right technology is a tricky thing that has to be done carefully not be three line discussions in a blog.

Please check out the Velocity documentation carefully, and in case of open questions rather contact the Velocity mailing list, which is typically very helpful in case of concrete questions.

Additionally I can recommend to make a fast prototype checking the critical factors using Velocity as the technology is easy to understand and use, and this can give you an impression if it will work for you.

Saifuddin said...

I wouldn't say more, but say thanks to you for replying me on my queries,

Thanks again, (wish me good luck for my project)

Alexander Schatten said...

Sure, I wish you goood luck!

Hope you stay tuned at our Blog, however, please use the appropriate community features of the very project (Velocity in that case), there you also find the experts that can help you with specific decision.