Blog

The jQuery fromTemplate plugin

December 21, 2009 11:43 in Javascript, Technology

We started using the Micro-Templating concept by John Resig to separate HTML markup from Javascript code in JSON based applications like our online crm solution. This concept solves the problem of outputting ugly strings of HTML very nicely, but the original code snippit was a little hard to understand for those of us who are not Javascript Ninjas (a book by John Resig). To fix this I had created a simple jQuery plugin and refactored the code a little.

This resulted in the following easy to use plugin:

$("#someElement").fromTemplate("templateId", dataset)

Where templateId is the ID of the template element and dataset is a simple Javascript object with data to expose to the template. Because this might help others understand the Micro-Templating code a bit better and might be useful to other jQuery users I have just uploaded the code to github as jQuery-fromTemplate-plugin.