-
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
Include index in each block helper, aka each_with_index #250
Comments
The index of the current array item has been available for some time now via {{#each array}}
{{@index}}: {{this}}
{{/each}} For object iteration, use {{#each object}}
{{@key}}: {{this}}
{{/each}} Here's a live example using the latest build: |
Any chance of also getting an option for a 1-based index, in addition to the 0-based? Perhaps something like this: |
What's your use case? |
Good point, mpetrovich, I should have included that. The use case is outputting a numbered array when <ol> isn't the best HTML choice. For example, I have a list of questions (contained in an array) that I want to output as a table of data (given that there are also questions associated to each one). So the output would be something like this (where the circles are radio buttons in the app): Select the best answer to these questions
Make sense? |
A generalized math helper might be an even better fit for your needs: http://jsfiddle.net/mpetrovich/wMmHS/ Then, you could just do:
|
I know this issue is closed, but would there ever be a case where you would output, to a 'regular' use, a list that started with 0? It may make sense as coders, but I can't imagine the general public wanting a list like that, and since this is for output, shouldn't it probably default to starting with 1? |
How do you use this when you use the {{#each foo in bar}} syntax? Seems like I get 'Cannot call method 'replace' of undefined' when trying to use it with that syntax. With the {{#each bar}} syntax, it works like it should. Any ideas? |
@perlun, could you paste the code in question? |
@mpetrovich adding a math helper is a bad good idea. The JSON model has to be modified in that case. |
@jokesterfr Yes, agreed. It'd be nice to just be able to do this: {{@index + 1}} |
@mpetrovich: Trying to do something like this. http://jsfiddle.net/plundberg/3c5Pv/ However, I'm not sure if the {{#each item in array}} is an EmberJS-extension or if this is supposed to work with Handlebars. (I'm using EmberJS in my "real" use case.) |
@perlun I don't quite follow—why doesn't the first (working) template suffice? |
I prefer to use the {{#each item in array}} syntax, since it doesn't pollute the context. Makes it easier to access stuff from the parent scope. That's why I tend to use the latter syntax. |
Just curious if there are any other know ways of accessing the key or index. the |
@Index is currently the only way to access the current array index. -Mike Sent from my iPhone On Mar 29, 2013, at 5:32 PM, Travis Dahl notifications@github.com wrote:
|
Is there a way to get the "index" or "key" of an upper level?
This does not work:
Thanks! |
@mickaeltr Unless we're using the wrong syntax, it looks like that's not possible yet: http://jsfiddle.net/Z9g2q/2/ Sounds like a great feature request :) |
@mpetrovich There we go: #491 |
For an Object like this:
Could we use the each helper to print it out?
Expected Output:
Doesn't seem to be working for me with Handlebars rc3 |
You need to wrap "this" in double curly braces as well:
Other than that, the syntax looks correct. On May 6, 2013, at 2:07 PM, Rajat Mittal notifications@github.com wrote:
|
@mpetrovich Failing JSFiddle: http://jsfiddle.net/gdXfN/29/ |
It would be nice to get an answer on the failing jsFiddle. Sorely missing this. |
I like the variables Django provides. So not just zero-based index but also others. |
Same problem as @lifeinafolder here. Any update available? :( |
+1 for @lifeinafolder -- running into the same failure in 1.0.0 |
I had to write a whole Handlebars helper:
Just to be able to do:
So that I could do:
This seems like such a painfully obvious use case: please provide one-based indices! No one uses zero-based indices in the presentation layer; human beings like 1-based indices :) |
Surprised this is still open. Would a pull request with this feature be ok? If so - I'll try taking a stab at adding it. |
How is this a backwards compat issue, Mickael? This would be a new On Sun, Jan 26, 2014 at 12:04 PM, Mickaël Tricot
Raymond Camden, Web Developer for Adobe Email : raymondcamden@gmail.com |
Alright, then why not! On 26/01/14 19:22, Raymond Camden wrote:
|
So - it was - afaik - one line change:
I then added
and it just plain worked. Maybe index1 would be nicer, but crap, it is time to end this thread. ;) Making a PR now. |
I would suggest to have same naming as Django. Because index is currently 0-based, maybe then |
👍 for {{@Index + 1}} |
I should point out - my PR was rejected. Afaik this is not going to happen. :\ |
Well that's good, because every developer I know shows their users: etc. |
For reference, @cfjedimaster's PR is #720. |
Upgraded to latest version of handlebars.js and it works! |
@steady-daddy Which syntax works? |
@jpdesigndev - @Index works inside #each block helper with handlebars v1.3.0 |
@steady-daddy Sorry. I hadn't paid close enough attention to the original issue. I was thinking you had gotten an |
@jpdesigndev - No issues mate! |
Ended up at this issue due to needing this exact features: @Index + 1 I don't understand how this has not been added; using a source-1 counting scheme is preferred for user-facing content in all cases. People do not start counting at zero, computers do. So, I solved this by installing the 'plusOne' @machineghost shared above. So, thanks, @machineghost; at least you helped in this issue. |
Glad I could help. I started a new ticket to try and get the developer's attention back to this use case; hopefully it result in a legitimate (ie. not my hack) fix. |
Another way to achieve this is to use CSS counters which are supported by all major browsers. |
Any way to set the start point for the index? The use case being setting a unique ID per row within view more paged UI? Below is a rough use case. Is there a way to set the index?
|
Common, why won't you modify the JSON object? var obj = { pages: [
{ id: "my-div-1", label: "Item 1" },
{ id: "my-div-2", label: "Item 2" },
{ id: "my-div-3", label: "Item 3" }
]} and use the following template:
What the point with |
If it helps, this was my solution. I edited the execIteration function within the each helper. handlebars-3.0.3, line 2375 ...
data.index = index;
data.humanIndex = index + 1; // added
data.first = index === 0;
... This could very easily be added in with a pull request. |
+1 for one based index |
Locking this thread as we are set on the implementation here. Zero-based index will continue to be exposed from the native implementation. Those that need a 1-based index should write a simple helper to add this value. With subexpressions that helper can be used as arguments to other helpers too so this provides the option to have one-based anywhere without the overhead of tracking another field in the iteration logic that may or may not be used or breaking existing templates.
|
It would be really nice to have access to the index of an each iteration.
There are ready-to-use block helpers out there, but having this upstream would be nice.
I can provide a pull request if that helps.
See also
https://gist.github.com/1048968
https://gist.github.com/1966803
http://stackoverflow.com/questions/5021495/in-mustache-how-to-get-the-index-of-the-current-section
The text was updated successfully, but these errors were encountered: