Skip to content

Instantly share code, notes, and snippets.

@dreadjr
Forked from malko/makeTemplate.js
Created September 22, 2017 19:01
Show Gist options
  • Save dreadjr/eb9a7ba38575f348bd02b726e46a3cb0 to your computer and use it in GitHub Desktop.
Save dreadjr/eb9a7ba38575f348bd02b726e46a3cb0 to your computer and use it in GitHub Desktop.
dynamic es6 template string to template methods
//const tpl = makeTemplate('hello ${name}')
//const name = 'world';
//tpl({name});
const makeTemplate = (templateString) => {
return (templateData) => new Function(`{${Object.keys(templateData).join(',')}}`, 'return `' + templateString + '`')(templateData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment