I want to be able to define templates once and use them to render html from both the server-side as well as the client-side. (DRY principle and all that)
The API that I'm envisioning is simply this: render(JSON, template) --> html.
I'm using a java-framework (actually Play framwork, but I don't think this is framework specific).
I've read a lot of similar questions, the latest, and most helpful being: Templating language for both client-side and server-side rendering.
I pretty much agree with the author that obvious contenders like: Mustache and Google Closure Templates are not going to cut it. (for reasons see that post)
Requirements:
I've seen a bunch of posts suggesting the use of Node.js for server-side templating. Although this would definitely work (underscore templates, Handlebarsjs, EJS would all work just fine) I'm struggeling to see how to communicate/combine/integrate Node.js with java, after all it's still the java framework that needs to output the JSON
I've seen posts mentioning some proof-of-concept communicating between a JVM and node.js (over http or using JNDI) . However, no library, let alone battle-tested, seems to be available at the moment.
So to round things up, what client-side templating engine would you suggest that would run in java as well (or with some hoops, can be called from a jvm) ? And if that 'hoop' happens to be Node.js, what ways of communication/ library would you suggest to use?
Client-side templating libraries allow you to create HTML with placeholders for your dynamic data. These libraries allow you to pass data to a template which will replace all instances of the placeholders in a template with the actual data. There are a few templating libraries out there including: Handlebars.
Before SPA frameworks came on the scene almost all apps were developed with using server side languages with templating systems: PHP, Ruby, Python, Java, C#, etc. Applications built this way rarely had complex client side logic or interaction that required writing lots of JavaScript code.
I'm going for Mustache for now and anticipating a java implementation for Handlebars.js. Once that exists, the refactoring-path shouldn't be that steep.
EDIT - april 2012
Ok, updating this for future reference:
100% DRY (even the client-side mixins and i18N-bundles come from the same source. Moreover, Hogan can precompile the templates server-side and open a connection to the client so the client doesn't have to parse the template anymore on first connect.
Is it fast? Lightning...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With