32

The net seems to be full of comparisons between Velocity and FreeMarker, and they seem to be reasonably equivalent. But there seems to be almost no comparisons between StringTemplate and FreeMarker!

So, what are the main differences between StringTemplate and FreeMarker?

My usage for them would be to generate HTML pages only. Out of the two, I would've expected FreeMarker to be the more suitable and more capable, as that seems to the more common one - but through a quick glance it seems that StringTemplate actually has more suitable features!

It would be great if someone who has used both would have time to comment - for the actual specifics, I can just read the documentation side by side, but I'd like something to get started with.

Charles
  • 50,943
  • 13
  • 104
  • 142
Nakedible
  • 4,067
  • 7
  • 34
  • 40
  • 1
    A quick clarification! I am not interested in the obvious differences - that StringTemplate is functional and has no loops etc and FreeMarker is almost a full blown programming language! I'm interested in concrete feature differences that come up during usage - where one thing is easily possible with one but really difficult with the other. – Nakedible Sep 18 '10 at 12:04

2 Answers2

39

I designed ST to build jGuru after getting sick of the "code in template" model of JSP. Velocity and friends (i.e., every other engine i think) give you more power than you need. I used basically four features to build jGuru.com (as described in paper). More features are unnecessary and lead you to entangle your model into the template. If you're building a one-off prototype, that's ok and any engine is fine. Code in templates is ok in that case since you don't care about maintenance.

Another thing people seem to ignore: how the hell can a graphics designer read code in templates? They can't. So, how can they work on the templates then? Even changing the order of some elements can break your model. You really need the separation to work in commercial environment, unless you want a site that looks like a coder built the html ;)

Terence Parr
  • 5,912
  • 26
  • 32
  • 4
    You don't know enough about ST to be commenting! ;-) But seriously though, I was more interested in the *other* features, besides the model-view separation. Stuff like template inheritance, which is present in ST but not in FM, atleast not in the same way. It seems very odd that nobody seems to take StringTemplate in to consideration when comparing FreeMarker, Velocity or even XSLT. – Nakedible Dec 11 '10 at 20:39
  • hiya. yeah, inheritance is critical to avoid duplicating templates. I use it extensively in ANTLR code generation. I have basic, say, Java templates to generate parsers. If you want trees, I have to inject tree construction code and w/o duplicating entire Java group. If you turn on -debug, I need to inject debug event triggers. Sometimes both! Inheritance lets you describe the difference between plain Java output and Java output with debug/tree actions. – Terence Parr Dec 11 '10 at 23:07
  • 3
    Inheritance is the thing that makes ST tempting! Otherwise, i don't like the overly strict mvc, as we have no mythical graphics designers that can't resist MVC changes and can't understand very simple VTL. – Nathan Bubna Jan 23 '12 at 16:12
  • It is, however, difficult to recommend using StringTemplate in a Servlet environment, because [it is not threadsafe, nor is there any public commitment to making it so](https://github.com/antlr/stringtemplate4/issues/61). – David Bullock Aug 24 '15 at 14:21
10

The main difference is that StringTemplate strictly enforces model-view separation and you can't put logic in the templates and FreeMarker lets you put full of logic in the templates.. This at least at the beginning make it more difficult to use but it's more scalable. For example if you are generating code and you have no logic in the templates generating another port for another language is less tedious because you haven't to replicate logic in each template.

There's a paper from Terence Parr that explains the benefits from model-view separation

lujop
  • 13,504
  • 9
  • 62
  • 95