Let’s start with a practical example. Say we want to give in multiple persons, and for each person you want to put in several benefits in kind for that person.
To create a fori-loop to put in multiple person, you can use fori-loop 1 :
{% fori dir in custom.directors %}
{% input dir.name %}
{% endfori %}
Now, for every person, I’ll want to make a second fori-loop to put in data. So I need something from fori-loop 1 that’s unique for each loop.
One can argue that’s the object name
but a better use is to use the key
part of the object, which is surely unique!
You can test this :
{% fori dir in custom.directors %}
{% input dir.name %} key is {{ dir.name.key }}
{% endfori %}
Each loop has its own unique key! This can be used to create our second fori-loop :
{% fori benefit in custom[dir.name.key] %}
{% input benefit.amount as:currency %}
{% endfori %}
So the custom collection custom[dir.name.key]
is created by the name-key we put in through fori-loop 1.
Example :
Code example :
{% stripnewlines %}
| {% t "Name" %}
| {% t "Benefits in kind" %}
| {% t "Amount" %}
|
{% newline %}
|----20%----
|----20%----:
|----10%----:
|----50%----+
{% fori dir in custom.directors %}
{% newline %}
| {% input dir.name %}
{% fori benefit in custom[dir.name.key] %}
{% newline %}
|
| {% input benefit.description %}
| {% input benefit.amount as:currency %}
|
{% endfori %}
{% endfori %}
{% endstripnewlines %}
A key can be added to any object, even the link between the collection and the input-object, for instance :
{{ dir.key }}
This too will result in an unique key!
Nested fori-loops
You could also use nested fori-loops as well, by using the key of an input object to create custom collections :
{% fori benefit in custom.benefits %}
{% input benefit.name %}
{% capture x_items %}x_{{ benefit.key }}{% endcapture %}
{% t "Overview benefits director" %} **{{ benefit.name }}**
{% fori x in custom.[x_items] %}
{% input x.name %} {% input x.amount as:currency %}
{% endfori %}
{% capture y_items %}y_{{ benefit.key }}{% endcapture %}
{% t "Overview benefits partner director" %} **{{ benefit.name }}**
{% fori y in custom.[y_items] %}
{% input y.name %} {% input y.amount as:currency %}
{% endfori %}
{% endfori %}
We capture the key-value into a custom collection, and use that to create nested fori-loops :
{% capture x_items %}x_{{ benefit.key }}{% endcapture %}
This is the result :