Mar 7, 2012

Rythm - an easy to use high performance Java template engine

I've kept looking for a template engine that has the rich features of Play!framework's groovy template engine, the speed of Japid and the clean, elegant syntax of Razor for years. Without any findings, finally I decide to create one by myself. And it comes Rythm.

I've used it for 2 months in real projects and can't feel any better than that. I'd like to share this work to public and start to promote it. This blog is to give a very brief introduce to Rythm by example. I found Mr. Philipp Schneider's blog on Velocity is easy to go and decide to rewrite it using Rythm. So let's have a look on how I use the template in Java:

Map<string, object> context = new HashMap<string, object>();
context.put("title", "template");
Person p1 = new Person("Mueller");
Person p2 = new Person("Schneider");
List<person> list = new ArrayList<person>();
list.add(p1);
list.add(p2);
context.put("personList", list);

String readyToUseTemplateOutput = Rythm.render("/folderinResource/MyTemplate.anyending", context);

You can also choose to pass template arguments by location:

Person p1 = new Person("Mueller");
Person p2 = new Person("Schneider");
List<person> list = new ArrayList<person>();
list.add(p1);
list.add(p2);

String readyToUseTemplateOutput = Rythm.render("/folderinResource/MyTemplate.anyending", "template", list);

And now the template file:

@args String title, List<person> personList;
Hello world!
This is my first @title
@for (Person person: personList) {
@if (person_index == 1) {Here is a list of persons:}
- @person.getName()
}

The result will look like this:

Hello world!
This is my first template
Here is a list of persons:
- Mueller
- Schneider

So this was really easy to use. And you can also create complex templates, that extends a layout template, invoke customized tags etc. You can do nearly all things that you can in velocity. Maybe even more.

In addition, Rythm is very fast, about 2 to 3 times faster than velocity. Some other features about Rythm:

  • Strong typed. Meaning your template get compiled and less runtime error
  • In-memory compilation. No need to call your javac to process generated java source files. Just call the API to process your template and get your result
  • Cached. Your template compilation get cached so next time no parsing/generating/compilation and it means super fast
  • Hot reload on dev mode. So if you are working on a server program you don't need to restart the server, just hit F5 to refresh your browser.
  • General purpose. Rythm can be used to generate any type of text file. It's not limited to HTML/XML
  • Easy to define tag/macro because every template is a tag and can be invoked from within any other templates
  • Support template inheritance. Reusing your layout page is piece of cake.

Check more on http://www.rythmengine.com!

Updates: I have just release an new template engine plugin built on top of Rythm for Play!Framework

About Me

My photo