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 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 parametrized text. Here is an example of using the system:
Though its possible to plug in any kind of template engine dialect, we can share the same API to invoke templates. e.g. we could create a Velocity / FreeMarker flavour TemplateEngine implemenation which could reuse GPath and auto-recompile to bytecode.
Using TemplateServlet to serve single JSP-like HTML files
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
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.
Article on templating with Groovy templates by Andrew Glover
Article on templating with Groovlets and TemplateServlets by Andrew Glover