-
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
Nested iterations and variable scope #803
Comments
I think I've got it, but had to create a modified "lookup" helper that extended its functionality a bit. While lookup works nicely if you're directly accessing the value you want printed, in my situation I needed to access a value within an object I'm looking up (more complex than my Toons example). Lookup works only on a single level. My helper handles additional arguments... <table>
<tr><th>Toons</th>
{{#each arr1}}
<th>{{name}}</th>
{{/each}}
</tr>
{{#each rowName}}
<td>{{this}}</td>
{{#each @arr1}} <!-- Solved this by implementing a helper that stores options.data value -->
<td>
{{myLookup this ../this "keyA"}} <!-- handles multiple arguments for accessing nested values -->
</td>
{{/each}}
{{/each}}
</table>
{{/each}} myLookup... Handlebars.registerHelper('myLookup', function(obj, field, options) {
var value = obj;
var fields = Array.prototype.slice.call(arguments, 1, arguments.length - 1);
fields.forEach(function(field) {
value = value[field];
});
return obj && value;
}); Perhaps this is an edge case but I can spruce up the above and make a PR if this is desired functionality for the lookup helper. |
In the 2.0 codeline we have the |
I'm trying to render tables, with the constraint that both my rows and columns are dynamically generated. I'm imagining a template that looks like this:
... Basically both the row header and column header are used as keys to index in to a value within an object. Using Javascript, I might achieve this by:
Which would produce a table like this:
Of course the point is to use some simple, flexible templating with reasonable helpers, and not do it all in Javascript. Does Handlebars support nested looping like this, with multiple indexes? Could this be implemented as a feature or helper?
Addendum: A portion of this seems to be addressed with #491. Going to investigate Handlebars 2.0 and see if I'm able to successfully perform the above...
The text was updated successfully, but these errors were encountered: