javascript - ES6 template literals based on template variable -
this question has answer here:
- convert string template string 14 answers
i try render es6 template literal variable :
function render(template, data){ ... } const template = 'resources/${id}/'; console.log(render(template, {id: 1})); // -> resources/1/
does exist way transform string template context formated string es6 template literals feature ?
you can not simple template literals.
however, can achieve such behaviour wrapping literals functions. es6 features (desctructuring , arrow functions), result code simple
function render(template, data) { return template(data); } const tpl = ({ id }) => `resources/${id}/`; console.log(render(tpl, { id: 1})); // resources/1/
Comments
Post a Comment