Skip to content

Instantly share code, notes, and snippets.

@lilac
Forked from malko/makeTemplate.js
Created March 29, 2018 12:11
Show Gist options
  • Save lilac/e5a2ccd05030008eaff4bb3328785f07 to your computer and use it in GitHub Desktop.
Save lilac/e5a2ccd05030008eaff4bb3328785f07 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