views:

3469

answers:

16

Do you know a better template engine than FreeMaker or Velocity?

+3  A: 

You can take a look at this link.

Shimi Bandiel
+5  A: 

StringTemplate is a template engine I'd like to try out someday:

http://www.stringtemplate.org/

Jonas Klemming
+1  A: 

A couple of my coworkers did a big investigation of Java templating engines just a couple weeks ago, and ended up choosing Freemarker. I've been very happy with it for the small amount I've worked with it, and my coworkers (who have done a lot more) seem very happy with it.

Herms
+1: I've had good results with FreeMarker, and don't understand the downvote on this.
Jim Ferrans
+13  A: 

I used StringTemplate in the Java Shop I worked before.

  • pure Java, lightweight, fast
  • simple to use
  • has formatting filters, which help to reduce application logic
  • a fit for every text based output: JSON, Xml, Html, Css

And the graphic department could learn it quite fast, because its simple.

The documentation is quite basic and doesn't have that many examples. But if you don't figure out a feature, the source-code is easy to read.

Andre Bossard
You forgot the most important: NO SIDE EFFECTS!
erickson
for Java this is the correct answer!
fuzzy lollipop
A: 

Struts Tiles works for us, but FreeMaker does seem to have a pretty nice implementation.

Kevin Day
+1  A: 

Another alternative: Groovy Templates. This allows you to use arbitrary Groovy code (very Java-like) within the templates.

flicken
and Groovy is very very slow compared to Java
fuzzy lollipop
A: 

I'd recommend XSLT. It's extremelly powerful.

XSLT it what Velocity tries to be, done right (it's pure XML, it's a pure functional turing-complete language, etc).

alamar
XSLT is designed for transforming XML into XML, and it should be used only for that. It is too heavy, too painful, and too limited to use for general templating needs. A template engine should not be Turing-complete, nor should it require XML (too heavy and obtuse).
Rob Williams
XSLT is a pain to use, any simple task requires lots and lots of xml.
mP
I also don't recommend XSLT, I've been doing e-commerce project in Java with XSLT transformations to produce HTML. We used Castor library to produce XML docs. XSLT was horrible pain and overkill, lines and lines of XML code. Really it is not worth to get into it.
Łukasz Korzybski
Conway's game of life is Turing-complete as well, but I wouldn't recommend using it as a template engine.
gregcase
+8  A: 

If you are investigating template engines, then I suggest that you have three primary questions:

  1. Does it support the language/platform that you are targeting (but maybe you should consider changing your target)?

  2. For each template engine, does it restrict you to any particular kind(s) of output (e.g., HTML) or can you generate any string of text? Once you comprehend the power of a template engine, you should/will want to use it everywhere that you manipulate complicated strings of text. The engine, therefore, should not require anything specific to a particular kind of output (like requiring a web server since it only generates HTML).

  3. Does it encourage you to create maintainable templates? This has several aspects to consider. If the engine is so complicated that it makes the task harder instead of easier, then don't bother (throw out XSLT, unless you are specifically translating from XML to XML, for which it was designed). If the engine lets you do ANYTHING beyond pushing simple data into the template, then throw it out. If the engine does not help you organize and modularize your templates, then don't bother. If the engine can't perform well, thereby slowing your application to a crawl, don't bother.

Having applied these criteria in a rather exhaustive search a few years ago, I settled on StringTemplate (and perhaps WebStringTemplate). So far, it is the ONLY engine to pass test 3. It readily passes test 2, but so do most engines. And it passes test 1 for my primary targets of the Python, Java, and .NET platforms (plus more).

I have used StringTemplate on all those platforms, and I have also used XSLT, Velocity, JSP (yuck) and several other engines over the years. StringTemplate wins--no contest.

You can save yourself a LOT of trouble by simply using StringTemplate. If you can't (platform not supported), then compare any candidates that you find to StringTemplate. And take particular note of any template engine that makes no effort to compare themselves against StringTemplate (like FreeMarker)--that is a huge red flag to me.

Rob Williams
A: 

WebMacro is another good java based template engine. There are many other good python language based template language.

We you many want to refer the following website to check the comparison matrix of various template engines

http://en.wikipedia.org/wiki/Template_engine_(web)

Arun ~ arunky

A: 

I prefer MiniTemplator. It is very simple yet powerful: you only have variable-placeholders and block-markers in your templates. Program logic like conditional blocks and loops are done in your host language (java), so you don't have to learn a new template-language.

MiniTemplator is available for Java, VB and PHP.

Florian
A: 

I am not recommending any templating language. But i just wanna get some inputs on how good a templating language should be. Basically whatever templating language you chose wont let you write dry code as you would do with other layers/tiers. The view logic is the most frustating to write down. I have few cluttered template scripts that are really really hard to maintain just because of few basic problems: 1. Grouping say we need to make groups out of list based on certain criteria. 2. Transforming data for example i need to say yes or no for boolean instead of true or false 3. This one is really really painful when you design navigations like you wanna disable link that point to current page. 4. You wanna show custom text for missing attributes etc...

I dont think any template language lets you cleanly do any of this. I am a novice may be its just my problem. But this problem is repeating on and on. I think these things shouldnt be handled anywhere else. And there are no best practices to do view logic too. Even if you try to follow all object Calisthenics with your java code, you end up writing very bad template logic. I wanna develop this conversation to yield some good inputs on how good a template engine should be

vinothkumar
+2  A: 

If you like the Scala programming language you might like Scalate as it allows you to use powerful Scala expressions instead of the limited JSP/JSF/JSTL EL expression language - while being completely statically typed so that templates are checked at edit/compile time for errors.

The Scaml templates in Scalate let you write really DRY templates which are particularly good for XML/HTML (Rails fans tend to love Haml and Scaml is the Scala port of Haml) - though if you're used to JSP you'd probably be better off starting with Ssp templates in Scalate which are JSP-like.

James Strachan
A: 

Try this one http://gwtapp.org/

gwtapp
A: 

Per your original question title:

Suggestions for a Java-based templating engine?

I shamelessly recommend a micro Java DSL I wrote for generating XML/HTML called JATL : Java Anti Template Language

I offer the recommendation since you still have not marked a correct answer.

Adam Gent
A: 

I can recommend XTC. It's a port of Facelets, that doesn't depend on JSF. Apart from some minor changes to schemas declared, it actually uses identical syntax. It's stateless, so it doesn't support features like components.

Its biggest advantages are:

  • Previewability, just like in Facelets
  • You can do both: composition and decoration, just like in Facelets.
  • It's pure XML, so it's actually best fit for website templating.

Neither Velocity nor Freemarker has any of those. StringTemplate supports 2nd point, but what put me off, were those group files and inheritence between them. I don't find it particularily useful in a field of webapp templating. I think it's been developed with language parsing in mind.

The solution is actually pretty stable, despite troubles actually getting to compile it. I spoke to the author and hopefully he will publish *.jar's as well. I also wrote a support for Spring MVC.