JavaScript Supplant Prototype
An example of using JavaScript to do parse and populate a template.
HTML
<div id="myDiv"></div>
JavaScript
if(typeof String.prototype.supplant !== 'function') {
String.prototype.supplant = function(o) {
return this.replace(/{([^{}]*)}/g,
function(a, b) {
var r = o[b];
return typeof r === 'string' ? r : a;
});
};
}
var template =
'<table border="{border}">' +
'<tr><th>Last</th><td>{last}</td></tr>' +
'<tr><th>First</th><td>{first}</td></tr>' +
'</table>';
var data = {
first: "Carl",
last: "Hollywood",
border: 2
};
myDiv.innerHTML = template.supplant(data)