$simple.template$
In most programs, we need to output some structured data - be it XML, a
property file or any other format. Generation of such data is usually
accomplished with use of control logic interspersed with print
statements. In these cases we are not generating random text - rather
we are outputting ‘statements’ in a specific language. Having a meta
language and an engine that allows us to convert a model from a program
into such a language greatly simplifies the effort. Template engines
like SimpleTemplate and associated languages accomplishes that task.
Why SimpleTemplate?
SimpleTemplate removes all the cruft that is part of other template
engines. It does one thing - that is converting a model into another
language - and does it well. SimpleTemplate assumes that the generated
output is some structured data and needs another tool to format it into
readable format if needed.
Motivation
In one of our projects we need to generate XML output from Hibernate
models. In our search we found StringTemplate
(http://www.stringtemplate.org) - that suited most of our needs - but
not quite.
Since the model objects are deeply nested we need a mechanism to create
an alias to refer to a deeply nested objects.
We need a way to index into arrays, lists and maps. The StringTemplate
mechanism of using rest and first to access the elements is cumbersome
and error prone in our context.
StringTemplate has lot more functionality than we needed and pulling in
Antrl to just use the templates just didn’t seem right.
SimpleTemplate uses a hand crafted, simple top down parser to parse the
templates.
Features
- Simple template directives.
- User selectable template directive separators.
- Alias creation using with and set directives.
- Support for indexing into arrays, collections, maps.
- Looping on an array or a collection.
- Simple include mechanism to reuse template files.
- Conditional evaluation using if and ifelse. No dangling else!
- Custom list separation settings (prefix, suffix and separators).
- Template files can be read from file system, class path or just a
string.
- Support for methods aka. sub templates.
How does it look like?
The following code just outputs the well known greeting.
package com.jaliansystems.simpletemplate.example;
import com.jaliansystems.simpletemplate.TemplateReader;
import com.jaliansystems.simpletemplate.Scope;
import com.jaliansystems.simpletemplate.templates.TemplateElement;
public class HelloSimpleTemplate {
public static void main(String[] args) {
try {
TemplateReader reader = TemplateReader.fromString("Hello $greeting$");
TemplateElement template = reader.readTemplate();
Scope scope = new Scope();
scope.put("greeting", "Simple Template");
String result = template.apply(scope);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Downloads, Sources and Support
SimpleTemplate github page is at http://www.github.com/kdmurthy/Simple-Template.
You can find latest sources and downloads from there.
Help and support are available from the SimpleTemplate google group at http://groups.google.com/group/simpletemplate.