javascript - ES6 template literals based on template variable -


this question has answer here:

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

Popular posts from this blog

Lists in Python -

android - can not access to progress bar in an other activity -

html - Not able to access next element of an array javascript -