CASE: how to create a fori-loop within a fori-loop

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 %}
{% 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 %} key is {{ }}
{% endfori %}

Each loop has its own unique key! This can be used to create our second fori-loop :

{% fori benefit in custom[] %} 
{% input benefit.amount as:currency %}
{% endfori %}

So the custom collection custom[] 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 %}
  {% fori dir in custom.directors %}
    {% newline %}
    | {% input %}
      {% fori benefit in custom[] %}
        {% 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 %} 
 {% capture x_items %}x_{{ benefit.key }}{% endcapture %}
 {% t "Overview benefits director" %} **{{ }}**
 {% fori x in custom.[x_items] %}
    {% input %}  {% input x.amount as:currency %}
 {% endfori %}
 {% capture y_items %}y_{{ benefit.key }}{% endcapture %}

 {% t "Overview benefits partner director" %} **{{ }}** 
 {% fori y in custom.[y_items] %}
    {% input %}  {% 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 :

Nice example … you were/are a basketball fan aren’t you :wink:?

Indeed I am @Bart_Verhaeghe :basketball_man:

Same here :slight_smile: … although I was more of a Utah fan (Malone-Stockton) …

1 Like

Hi Sven,

I have an additional question here:

Suppose that you keep the first fori-loop, but you’d like to replace the second fori-loop with a for-loop with checkboxes and with given options, such as:

{% assign str_benefits_possibilities = 'benefit A|benefit B|benefit C' %}
{% assign arr_benefits_possibilities = str_benefits_possibilities | split:'|' %}

And for each director, you’d like to put a check on a given benefit as well as an input possibility for the amount for the benefit in question.

Is it possible to put a forloop in a fori-loop following the same logic here?


Hi @Bart_Verhaeghe,

Little bit confused here as I read 2 different kinds of questions:

  • second fori-loop replaced by a forloop of options
  • a check and an input object for each director (fori-loop)

a check and an input object for each director (fori-loop)

this question is explained here in this topic, so not sure if you mean something else here?
The idea is you’ll need to create those database variables and attached them to something unique for each loop (the key attribute is the way to go here).

second fori-loop replaced by a forloop of options

So you want to display some check options only in the second fori-loop, always? You can do this, but I’m not sure if i follow your use case (sorry, it is Monday after all :wink:
Don’t you mean you’d like to be able to present some check boxes (which can be a forloop) for each director (fori-loop)?
What makes the second fori-loop so different from all others, may I ask (before we dive into this a little deeper)?

Just adding a small example of how you can nest a fori loop within a forloop:

Since for loops don’t have a key itself, we have to create one. One way of achieving this is by doing the following:

{% assign categories = "categorie 1;Categorie 2;Categorie 3" | split:";" %}
{% for categorie in categories %}
  {% capture categorie_key %}{{ categorie | replace:" ","_" | replace:".","_" | replace:"-","_" | downcase }}
  {% input custom.[categorie_key].name %}
  {% fori item in custom.[categorie_key] %}
    {% input %}
  {% endfori %}
{% endfori %}

Kind regards