Skip to content

Instantly share code, notes, and snippets.

@helloris25
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helloris25/f28b6a9e3e39d83ff48d to your computer and use it in GitHub Desktop.
Save helloris25/f28b6a9e3e39d83ff48d to your computer and use it in GitHub Desktop.
Simplest JS template engine
/*
* 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