I've written some days ago a simple templating function, problem now is that whitespace will kill the tag. So when I type something like this: <div id="str">#{my} #{name} #{is} #{#{a}}</div>
it works well, however the following does not: <div id="str">#{my} #{name} #{is} #{ #{a} }</div>
.
Here's what I've done so far:
$.tmpl = function(str, obj) {
do {
var beforeReplace = str;
for(var key in obj) {
str = str.replace("#{" + key + "}", obj[key]);
}
var afterReplace = str !== beforeReplace;
} while (afterReplace);
return str;
};
var map = {
my: "Am",
name: "I",
is: "<a href='#'>awesome</a>",
a: "#{b}",
b: "c",
c: "?"
};
$("#str").html(function(index, oldhtml) {
$(this).html( $.tmpl(oldhtml, map) );
});
How can i get it working if I use #{a}
and #{ #{a} }
.
I know, it is possible, and even simple, however I am not a regexp pro.
This works.
This fails.