Man cutting steel with a template (cc) Washinton State Dept of Transport
Underscore templates provide spaces where you can drop into javascript code - that's all they do, but it works better than trying to do more. Crucially, this gives you a lot of freedom - the full freedom of javascript.
For example, here is a simple loop using underscore:
<% for(var i=0; i<3; i++) print("
"); %>
That middle section isn't the prettiest, but it's something every web developer should already understand -- and know how to write.
Compare this with the approach taken in JQuery templates:
{{each}}
{{/each}}
This looks a little bit nicer -- but what are these new magic {{}} tags? Plus we need to pass in the list `[{i:1},{i:2},{i:3}]` in order to use it. And we have less ability to build richer more complex templates.
Hence, by taking a simpler code-based approach, underscore.js templates actually end up being more widely editable and more powerful.
No comments:
Post a Comment