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

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -