[心得] 一行的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