Some of you might have seen this PR I opened a while ago. Good news: it passed a superficial stage of code review and now is ready for testing. Please, try out this preview release with the following features and let us know what you think.
#each in
The new syntax to #each that introduces a new variable to refer to the current object, in comparison to the old syntax, doesn’t change the current data context.
<ul>
{{#each person in people}}
<li>{{person.name}}</li>
{{/each}}
</ul>
#let
#let allows you to introduce new variables-synonyms to other helpers or values in the data context.
<div>
{{#let name=getFullName age=this.bio.age}}
{{name}} is {{age}} years old.
{{/let}}
</div>
@index
The features above made the implementation of @index in #each a lot easier. You can use it like this:
{{#each person in people}}
<p>{{person.name}} - {{@index}}</p>
{{/each}}
How to test the preview release:
You can run Meteor with the --release EACH-IN@0.0.3. That’s it, you can create a new project by running meteor --release EACH-IN@0.0.3 create name-of-your-project or you can upgrade your existing project with meteor --release EACH-IN@0.0.3 update.
With the #each in syntax, you need to use #with to set the context if you want to access the state of the variable from within an event handler, right?
In this case it makes sense to me: template names and helpers and variables all share the same namespace. By writing {{> role role=role}} you refer to the closest definition of ‘role’, in this case, a variable defined in #each.
@slava Obviously spacebars is distinct from handlebars - but what was the reasoning behind diverging from the existing Handlebars Syntax- {{#each object as |value index|}} (See Handlebars Source)
Thanks for pointing this out! To be honest, I had no idea this is the official Handlebars syntax. I saw it in the Ember “future plans” docs, but not in Handlebars. I was referring to this page: http://handlebarsjs.com/block_helpers.html. Even current Ember implementation implements #each foo in foos, so this is at the very least, surprising.
After looking more into this and discussion this internally, here is what we found:
Handlebars syntax is not well documented. We consider features described in their documentation to be the “released” and perhaps “official” features.
Ember and Handlebars share the same Handlebars parser, although they have different syntax at this point.
Handlebars still uses the older syntax of each: #each things that changes the data-context
Ember introduced the newer syntax similar to what we propose in this thread: #each thing in things that doesn’t change the data-context.
Ember, the new unreleased version, is proposing the newest syntax of #each things |thing index|, which is not really loved by Ember community in their PR. It is not released yet.
There is no a word for Handlebars for changing their syntax, but their parser implements all of them as it is needed for Ember.
I like the fact that Spacebars is a familiar syntax to anyone who used Handlebars but the divergence is inevitable, and we can see how Ember and Ractive already diverge.