Bristle template: when you don’t have time to do your Moustache
Usually I generate my pages with Velocity. Recently I had a project where even implementing Velocity was not worth the time. I tried both MessageFormatter and StringTemplates and both had problems with some portion of HTML/Javascript markup.
So I did what we, engineers always do in this situation: run your own. Good news, it took just over an hour and I ended with a single very small file. It can only variable substitution but that was enough for me. Here it is:
The best part: it has no dependencies so you can just copy file into your project and template away.
Of course you can Maven just as well:
<repositories> ... <repository> <id>codebistro-open</id> <name>codebistro-open repo</name> <layout>default</layout> <url>http://codebistro.googlecode.com/svn/repo/</url> </repository> ... </repositories> ... <dependencies> ... <dependency> <groupId>org.codebistro.util</groupId> <artifactId>misc</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependencies>
Microframeworks are popular these days. This thing doesn’t even qualify to be called micro- or even nano- but I got a feeling I am going to use Bristle template in the future…
Burton 11:56 pm on 2010-03-09 Permalink |
codeibstro.com, how do you do it?
Sasha Ovsankin 10:53 am on 2010-03-11 Permalink |
Well the main advantage here to avoid duplication (DRY). Imagine a web page which is both rendered by the server (for SEO) as well as dynamically, via AJAX. Something like Twitter. If you do the AJAX rendering the usual way — get the data via JSON and then “dress it up” into HTML, you end up with 2 totally different pieces of rendering code which should be synchronized, a headache.
Using AHAH, you render both of them using the same code on the server side, transfer the rendered piece via AJAX and then the client-side JavaScript would only stick it at a proper place in DOM. Makes sense?
However since the time of this post things changed a bit: the HTML has become more “semantic” so the rendering magic shifted to CSS which can be safely reused by both sides. Also, we made some sort of browser-side templates: we put template in HTML with “display:none”, then for rendering we copy it, populate and make it visible. Still a bit of duplication but not really a big problem. So AHAH is not used that much any more.
Anton 6:40 pm on 2010-06-28 Permalink |
wikipedia link got deleted, that one worked fine for me
http://microformats.org/wiki/rest/ahah
codebistro 11:29 pm on 2010-06-28 Permalink |
Thanks, fixed the enry