Last active
December 12, 2019 04:15
-
-
Save lovetingyuan/e6ef9c86c2e32604040983e2a6b73c95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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