6

I'm looking for a template engine. Requirements:

  • Runs on a JVM. Java is good; Jython, JRuby and the like, too...
  • Can be used outside of servlets (unlike JSP)
  • Is flexible wrt. to where the templates are stored (JSP and a lot of people require the templates to be stored in the FS). It should provide a template loading interface which one can implement or something like that
  • Easy inclusion of parameterized templates- I really like JSP's tag fragments
  • Good docs, nice code, etc., the usual suspects

I've looked at JSP- it's nearly perfect except for the servlet and filesystem coupling, Stringtemplate- I love the template syntax, but it fails on the filesystem coupling, the documentation is lacking and template groups and stuff are confusing, GXP, TAL, etc.

Ideas, thoughts?

tshepang
  • 12,111
  • 21
  • 91
  • 136
alex
  • 5,213
  • 1
  • 24
  • 33
  • possible duplicate of [Suggestions for a Java-based templating engine?](http://stackoverflow.com/questions/174204/suggestions-for-a-java-based-templating-engine) – ripper234 Nov 22 '11 at 15:20

7 Answers7

2

How about Velocity?

  • full Java
  • does not require servlets
  • it has file, jar, classpath & URL resource loaders (and maybe more)
  • templates can include other templates (if this is what you mean)
  • has good tutorials, so far I could get what I needed from the docs
Péter Török
  • 114,404
  • 31
  • 268
  • 329
2

If my memory serves, FreeMaker is decent - Suppose to be some sort of "Velocity, the next generation".

Little Bobby Tables
  • 5,261
  • 2
  • 39
  • 49
0

What about Snippetory?

  • It's full java, (even your logic is written in java!)
  • Templates are loaded by the UriResolver, with implementations for calsspath, file system and URL.
  • It can be used outside Servlets, for example as ad hoc template like String.format.
  • It's entirely based on parametrized inclusion of templates.
  • The docs are ok and it's much simpler than the script based engines, so you really don't need that much.
  • And a new version with great new freatures is coming soon, I think
Bernd Ebertz
  • 1,317
  • 8
  • 10
0

Try Mixer2. http://mixer2.org/ Mixer2 has no dependency for servlet-API. You can store the template on String, java.io.File, and java.io.InputStream.

0

Chunk, my no-nonsense template engine for Java, would be a good choice.

  • Lightweight.
  • Great docs with examples and recipes: http://www.x5software.com/chunk
  • Standalone - no servlets required.
  • Templates from filesystem, classpath, strings, a webserver, or write your own template provider.
  • Based on simple templates with {$tags}.
  • Templates can include other templates or even execute them as macros with a json-formatted dictionary of tag values.
  • Looping, conditionals, filters, all the typical goodies.
Tom McClure
  • 6,699
  • 1
  • 21
  • 21
0

I'm the author of Pebble which fits your criteria quite well.

zb226
  • 9,586
  • 6
  • 49
  • 79
mbosecke
  • 842
  • 10
  • 22
  • I suggest elaborating on why this library would work for the user, at this time this is basically a link-only answer which is considered low quality. – Taryn Jul 24 '14 at 23:38
0

maybe check out "JSTP", http://jstp.sourceforge.net/manual.html

its syntax is subset of JSP, therefore IDE support is excellent.

a "jstp" template is translated into a plain java class at build time. there is no runtime dependency.

"parameters" to a template should be passed by member fields. static typing all the way.

Bar.jstp

<%!                                        
    public String name;                    
%>

Hello <%= name %> 

build converts it into Bar.java

public class Bar                                        
{                                                       
    public String name;                                 
    public void render(java.io.PrintWriter out)         
    {                                                   
        out.print("Hello ");                            
        out.print(String.valueOf(name));                
        ...                                             
    }                                                   
}  

and you invoke the template by

Bar bar = new Bar();                              
bar.name = "John";                               
bar.render(..);       

with typical "hotswap" you shouldn't have to restart serve when editing the template.

irreputable
  • 44,725
  • 9
  • 65
  • 93