Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 24 Next »

Template framework

Groovy supports multiple ways to generate text dynamically including GStrings, printf if you are using Java 5, and MarkupBuilder just to name a few. In addition to these, there is a dedicated template framework which is well-suited to applications where the text to be generated follows the form of a static template.

The template framework in Groovy consists of a TemplateEngine abstract base class that engines must implement and a Template interface that the resulting templates they generate must implement.

Included with Groovy are several template engines. Shown here is the SimpleTemplateEngine that allows you to use JSP-like scriptlets (see example below), script, and EL expressions in your template in order to generate parameterized text. Here is an example of using the system:

While it is generally not deemed good practice to mix processing logic in your template (or view), sometimes very simple logic can be useful. E.g. in the example above, we could change this:

to this (assuming we have set up a static import for capitalize):

or this:

to this:

Also just an advanced usage note. Because templates are created using two passes under the covers (pass one converts to script code, pass two runs the script to generate the output) you have to escape any backslashes inside GString expressions or scriptlet 'code'. E.g. if we wanted quotes around The Big Apple above, we would use:

Similarly if we wanted a newline, we would use:

in any GString expression or scriptlet 'code'. A normal "\n" is fine within the static template text itself. (Note: the necessity to do an extra slash may go away in a future version of Groovy if we can find an easy way to support such a change.)

Groovy also supports a GStringTemplateEngine (stores the template as writable closures) and XmlTemplateEngine (works well when the template and output are valid XML).

You can also plug in other templating solutions, e.g. GFreeMarker, Velocity, StringTemplate, Canvas and others.

Using TemplateServlet to serve single JSP-like HTML files

Mind the gap! Ehm, meaning the difference between Groovlets and Templates.



Error rendering macro 'link' : Link needs a name and a URL as arguments.
just works the opposite as the Groovlets(
Error rendering macro 'link' : Link needs a name and a URL as arguments.
) does. Here, your source is HTML (or any other, fancy template files) and the template framework will generate a Groovy script on-the-fly. This script could be saved to a .groovy file and served by the GroovyServlet (and the GroovyScriptEngine), but after generation, the template is evaluated and responded to the client.

Here is a simple example helloworld.html file which is not validating and does not have a head element. But it demonstrates, how to let Groovy compile and serve your HTML files to web clients. The tag syntax close to JSP and should be easy to read:

The first Groovy block - a for loop - spans the HelloWorld! text. Guess what happens? And the second Groovy statement prints the servlet's session id - if there is a session avaiable. The variable session is one of some default bound keys. More details reveals the documentation of

Error rendering macro 'link' : Link needs a name and a URL as arguments.

Here is some sample code using http://jetty.mortbay.orgs servlet container. With jetty6.0, copy jetty-6.1.3.jar and jetty-util-6.1.3.jar into $HOME/.groovy/lib,  create a tiny web server with the following. To test it, add your above helloworld.html file into your current directory and browse http://localhost:1234/helloworld.html

Here is a similiar web.xml example.

Further reading

Article on templating with Groovy templates
Article on templating with Groovlets and TemplateServlets
Blog about combining Groovy and FreeMarker

  • No labels