Created
April 18, 2013 03:41
-
-
Save joelsemar/5409922 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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