Re:T.js - A tiny templating framework in ~400 bytes
by petercooper on Monday August 13, 2012 @09:33PM
(#4378971)
This isn't for the whole thing, but a common pattern to clean up that inner part could be something like:
return { '>': '>', '<': '<', '&': '&', '"': '"' }[t] || t
All formatted nicer than that, of course ;-)
Re:T.js - A tiny templating framework in ~400 bytes
by jedschmidt on Monday August 13, 2012 @08:09PM
(#4378791)
new Option("<div>in the browser, yes.</div>").innerHTML
Re:T.js - A tiny templating framework in ~400 bytes
by jasonmoo on Monday August 13, 2012 @08:13PM
(#4378803)
Oh I like this. I was messing with a similar tactic appending a textNode to an HTML element but this is much more elegant.
Re:T.js - A tiny templating framework in ~400 bytes
by jedschmidt on Monday August 13, 2012 @08:25PM
(#4378827)
The text node approach is cross-browser, whereas I don't think this works on oldIE.
Re:T.js - A tiny templating framework in ~400 bytes
by jsharpe on Tuesday August 14, 2012 @12:27AM
(#4379409)
I just tried this and it doesn't seem to escape " to ".
Re:T.js - A tiny templating framework in ~400 bytes
by jfaucett on Tuesday August 14, 2012 @04:17AM
(#4379917)
yes! awesome snippit :)
Re:T.js - A tiny templating framework in ~400 bytes
by tedunangst on Monday August 13, 2012 @09:48PM
(#4379002)
You can make it 50% faster by observing that replacing > is unnecessary, and only one of < or " needs replacing in any particular context.
return s(val).replace(re, function(t){
});