Created
September 18, 2011 15:45
-
-
Save Integralist/1225181 to your computer and use it in GitHub Desktop.
@madrobby's 140 bytes template engine
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, data, prop) { | |
for (prop in data) { | |
string = string.replace(new RegExp('{' + prop + '}', 'g'), data[prop]); | |
} | |
return string; | |
} | |
/* | |
String templating engine: | |
t("Hello {name}!, It is {date}!", { name: "Thomas", date: function(){ return new Date }}); | |
// = "Hello Thomas!, It is Sun May 08 2011 15:15:33 GMT-0400 (EDT)!" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment