In Python, iteration gives me a named variable:
for cage in cages:
for animal in cage.animals:
print("The current cage is",cage)
print("the current animal is",animal)
In Ractive templates, I don't appear to be able to do this.
{{#cages}}
{{#animals}}
The current animal is {{.}} or {{this}},
but I don't know how to refer to the current cage,
i.e. the outer loop variable
I would like to be able to say {{cage}} has an {{animal}}
{{/animals}}
{{/cages}}
Is there a syntax I'm not aware of?
I've wondered about whether we should add an {{#each cage in cages}}
or {{#each cages as cage}}
syntax to handle cases like these. In lieu of that, mknecht's answer is totally valid, and something I often use myself; an alternative is to create an index reference like so:
{{#each cages :c}}
{{#each animals}}
The current animal is {{this}}, the current cage
is {{cages[c]}}
{{/each}}
{{/each}}
The difference with this method is that two-way binding will still work (though it doesn't sound like that's an issue in this case.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With