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

9 comments:

Unknown said...

A Play!framework plugin for rythm has been published.

Check it out here.

A comprehensive documentation of current version (0.9.4g) could be found at here

Antonio said...

like your work but... how to configure rythm properties like rythm.root and rythm.mode?
i want use your template engine for my webapp framework but i nedd more documentation. thx.

Unknown said...

If you are using Play!Framework, it's easy, just go to http://www.playframework.org/modules/rythm-0.9.4g/home, there are enough doc for you to use Rythm with Play. Unfortunately the document to use Rythm as stand alone template engine is rather poor though I hope I can find some time to work on it.

Back to your specific question, you can use the following code to init rythm configuration:

Properties p = new Properties();
p.put("rythm.root", new File(...));
p.put("rythm.mode", "dev");
Rythm.init(p);

You can also check this code to see how to configure RythmEngine:

Antonio said...

thx for your help! i really like rythm!

bendanpa said...

Will Play + rythm work on GAE (google app engine)?

Unknown said...

I am not sure if Play+Rythm works on GAE. I have no apps running on it. Probably you can have test and let me know the result

Unknown said...

Rythm now runs on GAE, check this demo app which is running on GAE

Kef said...

Great! Will you provide som integration with Spring MVC ?

Unknown said...

Here is an implementation of Spring integration:

https://github.com/lawrence0819/spring-web-rythm-template


It's not my work and I have used neither Spring nor the plugin. You need to evaluate by yourself