tags:

views:

174

answers:

7

I am looking for a lightweight template engine in Java. I want to be to give my users the ability to define a template in the line of:

"Some arbitrary text ${some_function(Some more text ${1}, function_param)} whatever"

And the template will be compiled and filled in runtime. Requirements are:

  • Template accepts array of objects as parameters (and extracts them using expressions like ${0} when 0 is the number of parameter)
  • Template has functions that modify their contents. For instance: ${trim(^^^bla bla ${0})} with argument ["bla^^^"] will return "bla bla bla" (^ = space. The site strips my extra spaces)
  • Functions can accept parameters. For instance ${substring(Whatever, 4, 8)} will return "ever".
  • Functions can be nested. A function operates on it's data after the inner function has been evaluated.
  • Functions are extensible, I want to be able to define my own.
  • My software is embedded in other production systems, so it has to be as small as possible (I don't want a gigantic framework with a tiny template engine that does what I need).
  • From that reason, performance is an issue. I can afford slow compilation time, but template-filling time has to be as fast as possible.
  • We use javaSE 1.4. Can't go higher, it's one of our features.

I don't think it's a very complicated requirement so I could write something like that myself, BUT since this is really a marginal feature in our product, I am reluctant to spend time on it, plus it seems like such a common use case, I'm thinking someone must have done this before.

I've looked at template-engines like Velocity and StringTemplate and FreeMarker. I think they are designed to produce really large documents with complicated templates, when performance is not an issue. I need something small, simple and fast.

Ideas?

+1  A: 

If your program deploys in JEE 6, then the Expression Language in JSP and JSF Facelets can do all this out of the box.

If not, then I believe you can get the Expression Language jars from the latest Glassfish and figure out how to glue them with your own application.

Thorbjørn Ravn Andersen
I've edited my post. We use javaSE 1.4 and we can't go higher since it's one of our features. I'll look at the Glassfish jars, though.
Hila
You could consider retroweaving the jars. Gives Java 5 facilites in a Java 1.4 runtime.
Thorbjørn Ravn Andersen
+4  A: 

Velocity from Apache is a wonderful templating engine.

duffymo
Isn't it a bit too heavy for my purpose? I mean, the common use case is something in the lines of my example. Not more than a 200 characters template...
Hila
Why should the size of the template engine be affected by the size of the template?
duffymo
What makes you think Velocity is heavyweight? I find it incredibly simple to use, fast, and powerful.
matt b
+1  A: 

I would recommend one of these two:

There are also bunch of other options (Spring EL, JBoss EL, Commons EL, OGNL) but I would consider them to be too heavyweight and slow for your needs (they all depend heavily on reflection).

In any case, you should create your own test and check the performance.

Neeme Praks
+1  A: 

I guess if you didn't like the big guns like Velocity, you could look at Google Closure Templates for Java.

It's pretty simple to use, but the integration with your tools is a bit... manual, for now.

See the "Concepts" section of the documentation I link to to learn what functions are readily available and how you can develop your own.

(sorry, being a new SO user, I can only post one link per answer and cannot link directly to all the things I mention. But you can jump there from that link by looking around the docs).

haylem
+1  A: 

http://freemarker.org/ is nice and easy to use.

David Winslow
+1  A: 

You should look at StringTemplate again.

zedoo
A: 

After profiling my application with the current banal implementation, I figured it's performance is actually good enough and it isn't worth the time incorporating a template engine into it.

Thanks for all your replies, guys.

Hila
That's fair enough and great for you, but if your question was about finding a template engine, you should have picked one of the answers actually offering a solution in that area. Plus you mention here as a reason for closing the question that performance is acceptable, but this was only one point in your question. Maybe it should have been phrased differently. Just a thought for future questions. :)
haylem
@haylem Fair enough, I removed the "accept" from my answer. Regarding the phrasing, I was looking for a list of template engines to evaluate.I don't know how I could have asked it differently.
Hila
@Hila: Well in that case I think it's fine, but from your question it seemed that the ease of use, lightweight payload and ALSO performance were factors for the selection, while your answer mostly addressed the performance issue by stating that what you had was already good. Thus the discrepancy. But otherwise I am perfectly fine with your question. Thanks for cleaning up.
haylem
One can also select more than one aswers; all those which were useful. Esp. for this list, there’s more than one helpful answer.
Kissaki