News for hackers. Stuff that matters.
Re:T.js - A tiny templating framework in ~400 bytes by jfaucett on Monday August 13, 2012 @07:53PM (#4378741)
looking at the replace chains on s(val) reminded me of something a while back, is there a faster/better way to do this king of stuff? something like:

return s(val).replace(re, function(t){

    if( t == '>' ) return '&gt';
    if( t == '<' ) return '&lt;';
    if( t == '&' ) return '&amp;';
    if( t == '"' ) return '&quot;';
    return t;
});
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 { '>': '&gt;', '<': '&lt;', '&': '&amp', '"': '&quot;' }[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 &quot;.
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.