Skip to content

Instantly share code, notes, and snippets.

@lovetingyuan
Last active December 12, 2019 04:15
Show Gist options
  • Save lovetingyuan/e6ef9c86c2e32604040983e2a6b73c95 to your computer and use it in GitHub Desktop.
Save lovetingyuan/e6ef9c86c2e32604040983e2a6b73c95 to your computer and use it in GitHub Desktop.
const context = {
title: 'hello',
list: [1, 2, 3]
}
const template = ' \
<header>${title}</header> \
<ul>${list.map(i => `<li>${i}</li>`).join("")}</ul> \
'
// note: make sure template and context are safe!
const templateFn = new Function(...Object.keys(context), `return \`${template.trim()}\``)
// "<header>hello</header> <ul><li>1</li><li>2</li><li>3</li></ul>"
templateFn(...Object.values(context))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment