Handlebars version update
It's a drop hard to tell what version of Handlebars the current templating system is based on. More recent versions of Handlebars support nice-to-have things like an @index context-dependent accessor so you can tag the index number of elements of an array you're {{each}}-ing through.
An update to the latest RC of handlebars would be great, as would some docs on how to Spark-ify our own versions of handlebars or other templating systems.
@sdossick, thanks for this. You're asking for three things here, all of which we should do:
- Better document the version of Handlebars that Meteor supports
- Extend Meteor's Handlebars implementation with the new features (@index sure does sound nice)
- Make templating system implementations fully pluggable (it's close but not quite there yet -- I suppose we need something like a 'language' attribute on tag, or determine it from the file extension?), and write a walkthrough on Spark-ifying your favorite templating system
I expect that we (the core team) will do them in roughly this order, but it may be a while before we get to all of them because we're pretty swamped right now.
We would happily take pull requests on (2). The only thing that Meteor uses from the official Handlebars distribution is the parser (we ship the templates to the client as an AST rather than complied code), so it's not as simple as pulling the latest upstream version of Handlebars, but I'd expect it to be fairly self-contained.
One odd thing is that it's surprisingly difficult to pretty-print a comma-separated list in Meteor/handlebars. @index would probably help this, because you could do:
{{each collection}}
{{if @index 0}}, {{/if}}
{{some_field}}
{{/each}}
which is a reasonable, if clunky, way to do such a simple task.
We would want to make sure that @index is appropriately reactive; normally Meteor can react to the ordering of a list changing without the data changing just by moving around DOM nodes without re-rendering, so we'll want to make sure that we re-render if the list nodes depend on @index.
Exactly -- that's why the simple "display a comma-separated list" task is so annoying currently -- you have to manage all those dependencies yourself. If @index is implemented "properly", then that's all done automagically.
(fwiw, i'm using CSS to add the commas between elements myself; ::after and :last-node are much easier than doing this properly with templates!)
I have been creating helpers for common tasks. Seems like this is also a fine solution for things you do a lot rather than some hacky code all over the place.
I did JSP for a long time and this is very common in the JSP world.
Example:
Handlebars.registerHelper('calendar', function (v) {
return moment(v).calendar().replace('last ','');
});
In the template:
{{calendar someDate}}
You could easily do the same for lists, something like:
Handlebars.registerHelper('commaList', function(list) {return list.join(',')}
@scottburch i'm pretty sure that doesn't actually work, due to the way meteor handles recursive template invocations. ie, I don't want to give the helper a list of strings, i want to use {{#each}} to generate document fragments from each instance of my collection, and then combine those.
See issue #499 for more details on the awkwardness.
You can work with collections in the helpers. I do it frequently. FYI. I do use recursive templates in a tree structure and I did have to do some trickery to get it to work, but it can. Maybe my example was bad, I was trying to duplicate your example.
yes, i know you can do it. my meta-point was just that "it's more painful than it ought to be", and by requiring custom helper functions starts blurring the distinction between presentation and code. The meta-meta-point is that a minimal template library like handlebars doesn't have very many features, but the ones it does have are there for a reason, and so i'm +1'ing the OP's request for a handlebars update.
Sounds good. I don't see it the same way since view helpers are still part of presentation and have nothing to do with "code". Just a point of view.
One more note that might help. My meteor app has more layers than the examples show. I have the html files and controllers. My controllers are still part of presentation because the real work is done in the model layer. That makes testing easier since I don't do testing in my controller layer. The controllers simply have view helpers and trap events. They then delegate any real work to the model layer.
This is more how Java style apps are designed.
+1
Would be really nice to see @index work.
+1
OK, so a challenge in implementing @index is making sure that it does the right thing with reactivity. Specifically, right now when you remove an element from a list or add one or move elements around, the DOM nodes for the other elements of the list are not re-rendered, just the relevant ones. (That's what Spark.list does.) If the DOM nodes depend on @index though, we'd have to re-render everything.
One possibility for implementing this: we detect for a given {{#each}} if its @index is actually used (deep icky Handlebars hooks...) and if so, we don't use Spark.list at all (ie, we just do the standard each over cursor.fetch).
+1
+1 I'd definitely be thankful for @index, @first, @last, @odd, @even and the like.
+1
+1, seems like a must for me. Need to be able to easily check odd, even, index, etc. Has anyone got a work round for this?
+1
+1