How to get named loop variable in ractive.js?

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 seem to be unable 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 any syntax that I don't know about?

+4
source share
2 answers

I wondered whether to add syntax {{#each cage in cages}}or {{#each cages as cage}}to handle such cases. Instead, McNecht's answer is fully valid, and I often use myself; an alternative is to create a link to the index like this:

{{#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 (although in this case this does not seem to be a problem).

+5
source

afaik, () : , . cage, . :

{{#cages}} {{# {cage: .} }}
  {{ cage.name }}
{{/}}{{/cages}}

JSFiddle

, Ractive , . . .

: . - , , .

+4

Source: https://habr.com/ru/post/1598991/


All Articles