Convert a string to a template string
As your template string must get reference to the b
variable dynamically (in runtime), so the answer is: NO, it's impossible to do it without dynamic code generation.
But, with eval
it's pretty simple:
let tpl = eval('`'+a+'`');
In my project I've created something like this with ES6:
String.prototype.interpolate = function(params) {
const names = Object.keys(params);
const vals = Object.values(params);
return new Function(...names, `return \`${this}\`;`)(...vals);
}
const template = 'Example text: ${text}';
const result = template.interpolate({
text: 'Foo Boo'
});
console.log(result);