StringTemplate is a great templating engine. It’s powerful, simple and quite opinionated. I’ve come really appreciate its simple purpose: render data. No assignment, no arbitrary method invocation. It is not Turing-complete and it would make a lousy rules engine.
SiteMesh is a web-page layout and decoration framework that is my current “golden hammer” when I need to provide a consistent layout across a java web-application. It seems to fit with the way that I think about web pages a whole lot more than something like Tiles – I really prefer decoration over composition as a means of layout control.
I’ve used StringTemplate it in the past on java projects and for a while have wanted to create a way to integrate it with Spring Framework’s MVC so I can stop using Freemarker and Velocity in Spring-heavy projects. Not that Freemarker is that bad, it’s just that I really don’t need or want all of the bells and whistles that come with it and allow it to be so frequently misused. All I really want is a templating engine that renders the model and does not get in my way. It just so happens that SiteMesh also makes an appearance on these projects and I’ve been wanting to use StringTemplate to provide layout decorators as well as Spring views.
Today I’ve released version 1.0 of my spring-stringtemplate integration library as a GitHub project. It provides an implementation of a Spring MVC View and ViewResolver for StringTemplate, and a decorator servlet for SiteMesh.
Feedback is always welcome.
#1 by Robert on 6 September 2010 - 8:44 pm
Quote
I’ve given up on all of these template engines. Now I just use Groovy – read gstring in from file, and go for it.
Works brilliantly.
#2 by Tom on 6 September 2010 - 9:01 pm
Quote
@Robert Agreed, unless of course you are in corporate-land and cannot use Groovy. Sometimes I have to push just to use Java 5.
#3 by GrahamBrooks on 7 September 2010 - 3:52 am
Quote
Nice one @Tom. I’ll take a look
#4 by ibra on 2 October 2010 - 6:02 am
Quote
Can you please show the steps to run the webapp ?
#5 by Tom on 14 October 2010 - 9:34 pm
Quote
@Ibra please pull the latest code from github and run: ant run-jetty
This should start up the sample application which you should be able to see at http://localhost:8080/stringtemplate/page/form
#6 by Davide Romanini on 9 November 2010 - 1:04 am
Quote
Nice project, but for sitemesh integration, the decorators templates have no access to the page model. I think StringTemplateView should expose the page model into request attributes and then populate the template *from* request attributes, so you can access the same model from page template and decorator templates.
Regards,
Davide
#7 by Tom on 9 November 2010 - 9:33 pm
Quote
@Davide Thanks for the feedback but I disagree on your point about page models in request attributes. I think that the page rendered by the StringTemplateView should provide a representation that is then further enhanced by a decorator and therefore should contain everything that the decorator requires. Passing this information through the page model as a request attribute does not seem to me to be the right way of doing things.
#8 by Davide Romanini on 9 November 2010 - 11:12 pm
Quote
@Tom Agree it’s ugly, but it’s the standard way with regular jsp decorators I think. I know you can pass parameters using meta tags, but I think that method is useful mainly when you deal with different technologies. In practice is simpler having access to the page model from decorators, since we are in the same request context and there’s no reason to complicate things further…
#9 by Tom on 10 November 2010 - 9:45 pm
Quote
@Davide I don’t believe that it is a standard, merely a short-cut to avoid proper separation of concerns. I’m using SiteMesh JSP decorators with Spring JSP views right now on a customer’s project and have not once required model attributes in a decorator. YMMV
#10 by Alex on 29 November 2011 - 2:45 am
Quote
Is that planned to release a new version of spring-stringtemplate that supprots String Template 4.x?
#11 by Tom on 29 November 2011 - 8:35 pm
Quote
I really don’t like the way that String Template 4.x requires that all templates have formal arguments. I’ve also been having a devil of a time getting the ‘$’ delimeter to work. ST3 is still my preferred templating engine and I don’t see enough benefits from ST4 to really interest me in spending any more time with it.