Last active
August 29, 2015 14:23
-
-
Save DanWebb/a95b4161ec47492ffff3 to your computer and use it in GitHub Desktop.
A very simple, small js templating engine
This file contains hidden or 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
// replace any occurences of str with a matching key:value pair | |
function replacements(template, replacement) { | |
return template.replace(/{([^}]+)}/g, function (match) { | |
return replacement[match]; | |
}); | |
} | |
// usage | |
var template = 'Hi { name }, you are { age } years old.'; | |
console.log( | |
replacements(template, { | |
'{ name }': 'Chris', | |
'{ age }': 50 | |
}) | |
); | |
// outputs | |
// Hi Chris, you are 50 years old. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment