Skip to content

Instantly share code, notes, and snippets.

@malko
Created March 21, 2017 11:03
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save malko/b8a432bbb2198ca5d38cd3dc27d40f24 to your computer and use it in GitHub Desktop.
Save malko/b8a432bbb2198ca5d38cd3dc27d40f24 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