[心得] 一行的Javascript template engine
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吧,寫程式真的開心很多。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.231.146.94
※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1536044827.A.0B6.html
→
09/04 15:38,
5年前
, 1F
09/04 15:38, 1F
一定要看原來的stackoverflow。
→
09/04 18:35,
5年前
, 2F
09/04 18:35, 2F
tagged template很好用
→
09/04 19:01,
5年前
, 3F
09/04 19:01, 3F
我覺得這東西還蠻神奇的。完全沒有從code generation的方向去想。
→
09/05 18:06,
5年前
, 4F
09/05 18:06, 4F
對對對,已修正。
→
09/05 19:59,
5年前
, 5F
09/05 19:59, 5F
我不需要currency, decimal, date的轉換。目前自己寫的只需要4k(還沒zip).
※ 編輯: oopFoo (61.230.117.180), 09/06/2018 00:07:02