-
-
Save antonioaguilar/786e89aa998320ff1217f379a8ec6683 to your computer and use it in GitHub Desktop.
Tiny 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
// 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