-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add @value to context when iterating over maps #1253
Conversation
Just throwing my 2c in to say 1) I agree with the premise that @value is more semantic. It's syntactic sugar for sure, but I can see how this could significantly increase template readability. I do think this warrants testing though, if possible? |
Absolutely agree. Who adds new features without writing tests? @giflw? 😛 |
Absolutely agree! I'll do as soon as possible. 😄 |
Tests done! Please let me know if needs more tests. 😄 |
Nice! Looking forward to this one. |
Hi! Is there a planned version to include this PR @nknapp ? |
That would be @wycats decision (new language feature). |
@giflw @nknapp I have no problem, in principle with adding this feature to the current feature set. However, I strongly believe that this set of features is best achieved in practice using the block-param feature: {{#each myObject}}
Key: {{@key}} Value = {{this}}
{{/each}}
{{#each myObject as |key value|}}
Key: {{key}} Value = {{value}}
{{/each}} The Ember dialect of Handlebars leans heavily on the block-params pattern and it has proven to be quite versatile. Here's an example from the Ember documentation: <ul>
{{#each-in categories as |category products|}}
<li>{{category}}
<ol>
{{#each products as |product|}}
<li>{{product}}</li>
{{/each}}
</ol>
</li>
{{/each-in}}
</ul> It is going to make sense for us to see about re-converging Handlebars with the exploration that went into the Ember dialect of Handlebars. Given the relative size of the Handlebars community, we will want to be very careful about any movement we make in that direction, so for the moment, I have no problem with this proposal. |
I'm a bit confused now. @wycats you say you have no problem with the feature because you want to converge with ember style loops (block params)? |
@nknapp if we're comfortable telling people to use the new block params form from now on, there's no need to add this feature 😄 I just didn't know if I could make a command decision like that in this thread, which is really about what seems like a very minor feature addition. |
I still think this is a good addition, as it's simple than the block syntax. |
@giflw when you have nested loops, it becomes more complicated than the block syntax: {{#each posts}}
<h1>{{@key}}</h1>
<div>{{@value}}</div>
<h2>Comments</h2>
{{#each comments}}
<h3>Reply to {{@key}} {{! oh no, @key has been overridden 😱 }}</h3>
{{/each}}
{{/each}} vs. the block syntax: {{#each posts as |title body|}}
<h1>{{title}}</h1>
<div>{{body}}</div>
<h2>Comments</h2>
{{#each comments as |commentTitle commentBody|}}
<h3>Reply to {{title}} {{! no problem I can easily refer to the outer title }}</h3>
{{/each}}
{{/each}} This kind of thing is why the block param syntax was added. It might take some getting used to, but it has no surprising "gotchas" when nesting things. |
I am not sure about this, but I tend to say: Use block syntax and do not add the It looks like a harmless addition on the first glance, but when I think about it: Strictly speaking, it is a breaking change. If someone has written a custom block helper that sets {{#myCustomHelper}} {{! sets "@value" }}
Value: {{@value}}
{{#each anObject}}
{{@value}}: {{this}}<br>
{{/each}}
{{/myCustomHelper}} We could merge it into I am reluctant to merge this, especially when there is another syntax to achieve the same goal. |
@nknapp Good point! And in fact, the motivation for block params is precisely the fact that this is a breaking change! I'm going to close this. |
Add @value to context to more semantic the access to map key/value pairs. In this way those both approach will hold the same result, but the second one beeing more semantic: