zoukankan      html  css  js  c++  java
  • 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 ?

  • 相关阅读:
    Linux--awk命令详解
    【python】将txt文本内容导入list列表
    【python】readlines( )函数的用法,读取文件内容
    【python】使用jieba分词并导出txt
    【python】module 'jieba' has no attribute 'cut'解决办法
    【python】UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position...解决办法
    pip安装python模块时报错,安装失败的解决办法,超详细!(Read timed out.等)
    【热力图】区域地图热力图,百度地图api
    【XAMPP】 Error: Apache shutdown unexpectedly. 11:00:50  [Apache] 解决办法详细
    Linux进入文件夹,查看文件,返回上级目录,查看列表文件(Ubuntu)
  • 原文地址:https://www.cnblogs.com/lexus/p/2524388.html
Copyright © 2011-2022 走看看