Platinum Partner
java,java templating velocity

Simple Templating System for Java

A templating system allows generating the text for HTML, emails, SQL, etc. without hardcoding the text in Java. This greatly improves the managability of your Java code and makes it much easier to make changes. While the templating system presented below is simple to implement, it still supports variable substitutions, condition logic, loops, etc. This templating system requires Jelly and is implemented in the provided JellyScript Wrapper Class.

Let's say you have a welcome email that should look something like this...

Hello, Kent, thanks for creating a Trendics account at www.trendics.com
Your new user name and password is...
   User: kent13600
   Password: my.secret
Be sure to checkout our webmaster tools at http://tools.trendics.com/.

- Trendics Support Team

Instead of hardcoding this text within a Java class, you can use the provided JellyScript Wrapper Class to define a template that looks like this...

Hello<jelly:if test='${!empty FirstName}'>, ${FirstName}</jelly:if>, thanks for creating a
Trendics account at www.trendics.com.
Your new user name and password is...
User: ${UserName}
Password: ${Password}
Be sure to checkout our webmaster tools at http://tools.trendics.com/.
- Trendics Support Team

The LastName and Password variables are simply substituted at the appropriate locations. The <jelly:if test='${!empty FirstName}'>, ${FirstName}</jelly:if> logic is only adding a comma and the FirstName if the FirstName is not blank.

As another example, you can use this templating system to generate SQL...

SELECT *
FROM Employee
WHERE State='${State}'
<jelly:if test='${!empty City}'>
AND City='${City}'
</jelly:if>

In the SQL template above, a State filter parameter is required and a City filter parameter may optionally be included.

The examples above did only simple variable substitution and a bit of conditional logic; however, Jelly supports a whole range of tags supporting various types of logic constructs including loops, creating Java objects, calling methods on Java objects, etc. (see here). The Jelly tags are very similar to JSTL tags commonly used in JSP pages -- this makes the learning curve easy for developers already familiar with JSTL.

Here is an example of the Java code necessary to execute a template...

 public static void main(String[] args) {

if (logger.isLoggable(Level.FINE)) logger.fine("main()");
try {
// Create a template
final String text = "";
text += "Hello<jelly:if test='${!empty FirstName}'>";
text += ", ${FirstName}</jelly:if>, ";
text += "thanks for creating a Trendics account ";
text += "at www.trendics.com. ";
text += "Your new user name and password is...\n";
text += "\n";
text += " User: ${UserName}\n";
text += " Password: ${Password}\n";
text += "\n";
text += "Be sure to checkout the instant website ";
text += "speed check at http://tools.trendics.com/sc.\n";
text += "\n";
text += "- Trendics Support Team\n";

// Create the JellyScript instance
final JellyScript jellyScript = new JellyScript(text);

// Create the variables needed by the template
final Map<String, Object> variables = new HashMap<String, Object>();
variables.put("FirstName", "Kent");
variables.put("UserName", "kent13600");
variables.put("Password", "my.secret");

// Execute the script to merge the variables with the template
final String result = jellyScript.execute(variables);
System.out.println(result);
}
catch (Exception e) {
logger.log(Level.SEVERE, "main()", e);
}
}

Note that defining the template within Java code sort of defeats a key benefit of templating -- you would normally define your templates within an XML configuration file or store the templates in their own files.

If you need a more robust templating solution, take a look at the Apache Velocity Project.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}