Skip to content

Instantly share code, notes, and snippets.

@DanWebb
Last active August 29, 2015 14:23
Show Gist options
  • Save DanWebb/a95b4161ec47492ffff3 to your computer and use it in GitHub Desktop.
Save DanWebb/a95b4161ec47492ffff3 to your computer and use it in GitHub Desktop.
A very simple, small js templating engine
// 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