javascript - Can ES6 template literals be substituted at runtime (or reused)? - Stack Overflow

PHOTO EMBED

Wed Jan 26 2022 11:00:00 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol

const templateString = "Hello ${this.name}!";
const templateVars = {
    name: "world"    
}

const fillTemplate = function(templateString, templateVars){
    return new Function("return `"+templateString +"`;").call(templateVars);
}

console.log(fillTemplate(templateString, templateVars));
content_copyCOPY

https://stackoverflow.com/questions/30003353/can-es6-template-literals-be-substituted-at-runtime-or-reused