jQuery.grab and Beyond – JavaScript Framework Thoughts Part 2
Templating
So, Backbone uses Underscore templates, which I never really used much because I’m such a huge Mustache fan. Anyway, like I said in the last post I’ve been using Ractive for templating a good deal for work, so I’ve been thinking about what a template might look like if it were in a framework that I wrote.
I sort of dabbled with this before… about three years ago with a jQuery plugin called jQuery.grab. The idea is, jQuery reads your grab template creates all the elements and wraps select elements in a jQuery object and automatically makes them properties of a given class (constructor object).
Here is the old github for jQuery.grab:
https://github.com/ZevanRosser/jquery.grab
The demos show how it works:
https://github.com/ZevanRosser/jquery.grab/tree/master/demos
So jQuery.grab isn’t really a template system but it enables views to be created and have relevant elements selected and wrapped in jQuery objects – which is cool and at the very least makes it so you don’t have a million selectors in your code.
So the extension of this is templates that can be bound to the type of object mentioned in the previous post. Additional, these templates should be smart and do as little dome manipulation as possible. Once an element gets created, it gets updated instead of recreated and for collections nodes should be appended if possible or smartly selected and removed.
My first stab at this was just to create the syntax for an ideal template. This will likely change as I continue to write the javascript but here is the first pass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <script id="tmpl" type="text/html"> <div class="app"> <h2>.title</h2> <p>.some.nested.prop</p> <form> <input type="checkbox" checked=".checked"> </form> <ul data-items> <li>.value</li> </ul> <select data-files> <option value=".id">.fileName</option> </select> <div data-names> <div class="name">.</div> </div> <div data-collection> <div class='thing'> <h2>.title</h2> <div data-things> .thing </div> </div> </div> </div> </script> |
The idea here is that you would never include actual text content, but always have it in your JSON. Variables start with a . and collections are just defined by a data attribute where the second part of the attribute is the path to the corresponding collection, like this:
// our data and corresponding icons collection... { settings : { icons : [ {src : 'someImage.jpg', name : 'some image'}, {src : 'someOtherImage.jpg', name : 'some other image'} ] } }
… and our html:
1 2 3 | <div class="items-collection" data-settings.icons> <img src=".src" /><span>.name</span><br/> </div> |
Something like that…
The cool thing with this is that the template will always be completely valid html so to build the template with javascript you can just use innerHTML on some div, then recursively process the nodes of the template to populate and data-bind them.
I’ve started work on the javascript for this but haven’t gotten it very far yet.