Say I have template literal like so:
const templateLiteral = `string text ${expression} string text`
I want to dynamically evaluate the template literal into a finished string.
function toFoo(templateLiteral){
//returns "string text Foo string text"
return templateLiteral.evaluate('Foo');
}
function toBar(templateLiteral){
//returns "string text Bar string text"
return templateLiteral.evaluate('Bar');
}
function toBaz(templateLiteral){
//returns "string text Baz string text"
return templateLiteral.evaluate('Baz');
}
is there a way to do something like this with template literals, or am I just being dumb? (template.evaluate() is a made up function, but I am looking for that kind of functionality with JS!).
See Question&Answers more detail:os