use groovy as structs2 template engine

Cedric Champeau's Weblog : Weblog

Replacing Velocity with Groovy = JSmarty ?

12:14PM Aug 01, 2007 in category Java by Cédric Champeau

Tags: groovy smarty velocity

Templating languages as first choice technology for front-end customization 

I'm not fond of JSF-like front end technologies. I mean, for anyone who's used to templating technologies such as Smarty for PHP, using JSF is masochism : crying things like you can't just iterate over a simple collection. With JSF, you must understand the underlaying philosophy and the technology before you succeed in doing something as simple as echoing a set of records. Too complicated for simple designs, and, nonetheless, unusable by a web designer (I mean someone who's able to write HTML, but knows almost nothing of programming).

That's why I like templating technologies : simplicity, and with a good application design, you won't break the MVC model. Customizing a web design for the needs of a customer would not require anything else than changing a few front-end templates. For this I've been using Apache Velocity in Java, while in PHP I was used to Smarty.

Velocity's main drawback 

But Velocity does not offer the softness Smarty has : imagine your web designer needs a date format tag in order to pretty print a date variable. In Smarty, you just need to write a plugin (ok, for dates it is just included ;-)), and it is straight available in your templates. With Velocity, you must imagine all usages of the variables or objects you expose before you make a template. I mean that you must think that a web designer would like to format a date, then expose the date utility to the template.

In this case, in order to be sure you won't forget anything, you could simply expose all of your utility classes to the template, but this is fondamentaly unnecessary, ugly and you could possibly never imagine everything a template writer could need. The problem is right there : with Velocity, if you did not think of something at the design time, then it will not be possible to do at runtime, just because template variables and utility classes are exposed programmatically. It is just right for variables, but a problem for utility classes. That's the main drawback I found to Velocity.

Then came Groovy and its SimpleTemplateEngine. Basically, it does the very same as Velocity for templating, but its main advantage is that you can embed groovy code into the template :

import groovy.text.SimpleTemplateEngine

def tpl_src ='''
<% def formatDate(date) {
    new java.text.SimpleDateFormat("EEE, MMM d, ''yy").format(date)
}%>

The date today is <% print formatDate(date) %>'''

def binding = [ "date": Calendar.getInstance().getTime() ]
def engine = new SimpleTemplateEngine()
println engine.createTemplate(tpl_src).make(binding)

Just as simple as that ! You won't need to rebuild an application in order to make an utility class, which has fundamentally nothing to do with the application logic, available to a template (front end logic). That's where I liked Smarty so much, and makes me think I should actually think of replacing my Velocity templates with Groovy ones.

 Should I move ?

Well, before moving, I'll have to double check things like :

  • performance : how does Groovy templates compare to Velocity (Smarty has this caching thing)?
  • template inclusion : how can I manage template inclusions like velocity does ?
  • security : beeing able to run arbitrary code in the template may not be a so good idea...

Well I have a semi-answer for the last one : GSP pages, built upon Groovy, seem to have a <g:include> tag, but GSP's are part of the grails framework, which is too large for this particular need.

What do you think ?

posted on 2012-05-29 18:11  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/05/29/2524388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值