Skip to content

Instantly share code, notes, and snippets.

@datfaf
Forked from couchoud/supplant.js
Created September 4, 2011 01:18
Show Gist options
  • Save datfaf/1192063 to your computer and use it in GitHub Desktop.
Save datfaf/1192063 to your computer and use it in GitHub Desktop.
basic hs templating using String object and Regex
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;
});
};
}
/**
usage:
mydiv.innerHTML = template.supplant(data);
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
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment