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?