Created
November 12, 2014 04:35
-
-
Save kayue/abb71a6db0adc89d0614 to your computer and use it in GitHub Desktop.
JS 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 t(s,d){ | |
for(var p in d) | |
s=s.replace(new RegExp('{'+p+'}','g'), | |
typeof d[p][1]=='f' ? d[p]() : d[p]); | |
return s; | |
} | |
t("Hello {who}!", { who: "JavaScript" }); | |
// "Hello JavaScript!" | |
t("Hello {who}! It's {time} ms since epoch.", { who: "JavaScript", time: Date.now }); | |
// "Hello JavaScript! It's 1299680443046 ms since epoch." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment