Skip to content

Instantly share code, notes, and snippets.

@antonioaguilar
Forked from anonymous/index.html
Last active April 3, 2016 17:31
Show Gist options
  • Save antonioaguilar/786e89aa998320ff1217f379a8ec6683 to your computer and use it in GitHub Desktop.
Save antonioaguilar/786e89aa998320ff1217f379a8ec6683 to your computer and use it in GitHub Desktop.
Tiny Template Engine
// tiny template engine function
function tpl(str,obj) {
for(var key in obj) {
str=str.replace(new RegExp('{' + key + '}','g'), obj[key]);
}
return str;
}
// code example
console.clear();
var params = { name: 'John', language: 'JavaScript', skills: 'coding' };
var query = 'Hello {name}, do you want to learn {language} {skills}?';
var res = tpl(query, params);
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment