4 ms·
I like it. Here's a slightly more stable version. But still not crazy about using eval on arbitrary code. O.O function render(tpl,data){ retu
by jasonmoo 14y ago
I like it. Here's a slightly more stable version. But still not crazy about using eval on arbitrary code. O.O
function render(tpl,data){
return tpl.replace(/{([^\}]+)}/g,function(_, key) {
try { return eval("data."+key); }
catch (e) { return ""; }
});
}
- vain 14y agothe code i use in production doesn't use eval, but a recursive function. i like the anonymous function passed to replace, but think the catch destroys code beauty :)