Skip to content

Instantly share code, notes, and snippets.

@joelsemar
Created April 18, 2013 03:41
Show Gist options
  • Save joelsemar/5409922 to your computer and use it in GitHub Desktop.
Save joelsemar/5409922 to your computer and use it in GitHub Desktop.
function Template(string){
this.string = string;
this.render = function(){
var args = Array.prototype.slice.call(arguments);
if(args.length === 1 && typeof args[0] === 'object'){
return this.string.replace(/{([^}]*)}/gm, function(match,key) { return args[0][key] });
}
return this.string.replace(/\{(\d+)\}/g, function(match, idx){ return args[idx] });
}
}
var myTemplate = new Template("Hey {0} what the fuck are you doing to my {1}");
myTemplate.render("Joel", "bannana");
//"Hey Joel, what the fuck are you doing to my bannana"
var myTemplate = new Template({"Hey {name} what the fuck are you doing to my {fruit}")
myTemplate.render({name: "Joel", fruit: "banana"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment