function render(tpl,data){
var matches = tpl.match(/{[^\}]+}/g);
for(var i in matches){
var rep = eval("data."+matches[i].replace('{','').replace('}',''));
tpl = tpl.replace(matches[i],rep);
}
return tpl;
}
alert(render("{a} is all that i've {b.a} {b.b} {b.c}", {a:"this", b:{a:"ever",b:"really",c:"needed"} }))
the 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 :)
render("{a} is all that I've {a}", {'a' : 'b'});
"b is all that I've b"
render("{a} is all that I've {{a}}", {'a' : 'b'});
SyntaxError: Unexpected token {
function render(tpl,data){
var matches = tpl.match(/{[^\}]+}/g);
for(var i in matches){
var rep = eval("data."+matches[i].replace(/[{}]/g,''));
tpl = tpl.replace(matches[i],rep);
}
return tpl;
}