Skip to content

Instantly share code, notes, and snippets.

@jaseemabid
Created October 28, 2011 04:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaseemabid/1321623 to your computer and use it in GitHub Desktop.
Save jaseemabid/1321623 to your computer and use it in GitHub Desktop.
JavaScript supplants
/* Supplant for templates and data filling */
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;
});
};
}
/* You need a template and data */
template = "this is {data} "
data = {"data" : "value"}
result = template.supplant(data);
/* Result will be "this is value" */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment