Hi guys,
I have just committed a new plugin to the trunk. It's a simple templater
that allows you to create "templates" that get populated via JSON objects.
Some examples:
var tmp = $.makeTemplate("My name is {{foo}} and your name is {{bar}}");
tmp({foo: "Yehuda", bar: "jQuery"}) #=> "My name is Yehuda and your name is
jQuery"
It also works with global functions and objects:
var tmp = $.makeTemplate("The number being passed in is {{number}} and its
integer value is {{parseInt(number)}}");
tmp({number: 17.6}) #=> "The number being passed in is 17.6 and its integer
value is 17"
In typical jQuery style, you can make it work on elements too:
<div id='testMe' template='{{first}} {{last}} {{parseInt(Math.sqrt
(number))}}'></div>
$("#testMe").updateTemplate({first: "Yehuda", last: "Katz", number: 17});
Result: <div id='testMe' template='{{first}} {{last}}
{{parseInt(Math.sqrt(number))}}'>Yehuda
Katz 4</div>
You can also load in the contents via a new Ajax request:
<div id='testMe' template='{{first}} {{last}} {{parseInt(Math.sqrt
(number))}}'></div>
$("#testMe").loadTemplate("test.json");
Result: <div id='testMe' template='{{first}} {{last}}
{{parseInt(Math.sqrt(number))}}'>Yehuda
Katz 4</div>
assuming, of course, that test.json contains the JSON object passed in to
updateTemplate above.
You can check out the plugin at
http://jqueryjs.googlecode.com/svn/trunk/plugins/templating/
It already has a few unit tests, which serve as a simple demo for how it
works.
--
Yehuda Katz
Web Developer | Procore Technologies
(ph) 718.877.1325