仓库地址:https://github.com/hustcc/tplv 请勿用于生产环境,写本地工具之类的可以试试。
要什么仓库地址,就下面 8 行代码。
/** * nano tpl library * @param template * @param data */ export default function(template: string, data: object): string { const ks = Object.keys(data); const vs = ks.map((k: any) => data[k]); const t = `return \`${template}\``; const f = new Function(...ks, t); return f(...vs); } 模板语法就是 ES6 String template 的语法。
import render from 'tplv'; const template = '${ name }, ${value}(${percent} | Top ${array[2]})'; const data = { name: 'Hangzhou', value: 1200 percent: '13%', array: [1, 2, 3, 4] }; render(template, data)); // will got `Hangzhou, 1200(13% | Top 3)` 