[心得] 一行的Javascript template engine

作者: oopFoo (3d)   2018-09-04 15:07:04
https://stackoverflow.com/questions/30003353/can-es6-template-literals-be-substituted-at-runtime-or-reused
function fillTemplate(templateString,templateVars) {
return new Function(`return \`${templateString}\`;`).call(templateVars);
}
因為需要做多國語言(前端做),找類似printf功能來置換value。
有看到,但都不滿意,因為想用es6 template literal來做。
問題是怎麼用string variable來當做template literal的置換?
真的有神人po出解決方案。很久沒這麼開心了,這行程式碼,看了兩三遍才看懂。
心得:
趕快用es6吧,寫程式真的開心很多。
作者: ddoll288 (風兒卿卿)   2018-09-04 15:38:00
直接看stackoverflow比較清楚
作者: fukinhot (抱歉粗口我怕熱)   2018-09-04 18:35:00
做得到 關鍵字:tagged template
作者: dreamnook (亞龍)   2018-09-04 19:01:00
啊哈哈這招我有在我開發Game的時候用過xD
作者: weiyucsie (選擇那刻 才算開始)   2018-09-05 18:06:00
是不是要\`${templateString}\` ?
作者: justben (BEN)   2018-09-05 19:59:00
為何不用i18n 很多套件啊

Links booklink

Contact Us: admin [ a t ] ucptt.com