JavaBeat
Search JavaBeat

JAVABEAT
home
articles
tips
code junction
QnA
Books
forums
ARTICLE TOPICS
All Articles
Java 5.0
Java 6.0
EJB 3.0
JCA
Struts
JSF
Spring
Groovy
JBoss Seam
Hibernate
Eclipse
JavaFx
Google Guice
J2ME
GWT
WebServices
AJAX
ARCHIVE
2007 | 12 11 10 09 08 07 06 05 04 03
2008 | 04 03 02 01
CERTIFICATION KITS
350 SCJP 1.5 Mock Exams
400 SCJP 1.6 Mock Exams
300 SCWCD 5.0 Mock Exams
300 SCBCD 5.0 Mock Exams
Enter email address:

Latest JavaBeat Articles Delivered by FeedBurner
Sponsors
Java Jobs Get Firefox 2! Linux Reviews Get Ubuntu 8.04  

PacktPub Publication Java Books
Pages : 1 2

Templates in Groovy

Author : Raja
Date : Sun Apr 6th, 2008
Topic : groovy  
Title : Templates in Groovy
Publisher : Raja

1) Introduction

In this article, we will expose the various APIs for processing Templates in Groovy. A template can be thought of some static content along with some well-defined place-holders. These templates can be re-used any number of times by simply copying it and substituting the place-holders with some appropriate values. The first section of the article concentrates on the various types of Template Engines available in Groovy. And the later section of the article guides you in using the Template API. This is not an introductory article about Groovy, so novice readers can read the introductory article Introductory Article on Groovy before continuing with this article.

2) Groovy Template API

2.1) Templates

As mentioned earlier, generally a template is a static content with well-defined placeholders. For example, consider the following,

'[0] and [1] went up the hill'

The above content is an example of templated content which is having two place-holders pointed by [0] and [1]. Now this templated content can be re-used any number of times as following by substituting appropriate values,

'Jack and Jill went up the hill' 'Mary and Rosy went up the hill' . . . 'Somebody and Nobody went up the hill'

2.2) API Support

The necessary support for programming Groovy templates is available in the groovy.text package. This package can be logically broken into three pieces as follows,

  1. Representation of a template
  2. Template Engines

We will discuss in detail in the forth coming sections.

2.2.1) Representation of a template

Template in Groovy is represented by the interface groovy.text.Template. A template usually represents some text content and they are often associated with a set of bindings. The set of bindings contains the values for the template text containing place-holders.

Values can be passed to the template text by calling the Template.make(Map bindings) method.

2.2.2) Template Engines

Template Engines are Factory classes which are used to create Template objects from a variety of sources. The source can be as simple as an ordinary text, from a file or a reader or from an URL. For example, the following code snippet creates a Template object whose content will come from a file,


File fileContainingTemplate = new File("template.txt");
TemplateEngine templateEngine = new SimpleTemplateEngine();
Template templateObject = templateEngine.createTemplate(fileContainingTemplate);

 
Pages : 1 2
 

Sponsors
Webmaster Hosting Forum
Java Jobs
Latest in DLinks
http://javawave.blogspot.com/2008/04/quartz-job-scheduler-part-ii-example.html
Quartz Job Scheduler -- Part 1 (Setting up development project in Netbeans 6.1 beta)
Flex Messaging with BEA Workshop Studio
SOA and Virtualization: How do They Fit Together?
Introduction to Enterprise Portals - Why they Benefit IT and the Business
BEA WebLogic Operations Control: Application Virtualization for Enterprise Java
Best Practices for Building Production Quality EAR Files
BEA's SOA Reference Architecture - A Foundation for Business Agility
Blueprint for Successful SOA Integration
Guardian - What a tool!

JavaBeat Media (2004-2008), India
javabeat home | About Us
our network : opensource softwares
Copyright © 2007 JavaBeat