Last active
August 29, 2015 14:27
-
-
Save helloris25/f28b6a9e3e39d83ff48d to your computer and use it in GitHub Desktop.
Simplest 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
/* | |
* String nanoTpl (String template, Object data) | |
* | |
* Usage: nanoTpl('Hello ${name}! (not ${name}?)', { name: 'Gandalf' }) | |
* | |
* Result: Hello Gandalf! (not Gandalf?) | |
*/ | |
function nanoTpl (template, data) { | |
var result = template; | |
for (var field in data) { | |
var re = new RegExp( '\\$\\{' + field + '\\}', 'gi' ); | |
result = result.replace(re, data[field]); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment