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

**URL:** <https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802>\
**Category:** Cases\
**Tags:** iterations, fori\_loop, use\_case\
**Created:** [April 4, 2018, 12:37pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802 "2018-04-04T12:37:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sven](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/sven/32/1495_2.png) [@sven](https://community.silverfin.com/u/sven)\
**Post date:** [April 4, 2018, 12:37pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802/1 "2018-04-04T12:37:24Z")

</div>

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 :

```auto

{% 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 :

```auto

{% 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 :

```auto

{% 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 :

 ![05](https://canada1.discourse-cdn.com/flex030/uploads/getsilverfin/original/1X/98a138e0f84a0d78cb13b0f0e70e28daa02283a2.png)

Code example :

```auto

{% stripnewlines %}
| {% t "Name" %}
| {% t "Benefits in kind" %}
| {% t "Amount" %}
| &nbsp;
{% 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 :

```auto

{{ 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 :

```auto

{% 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 :

```auto
{% capture x_items %}x_{{ benefit.key }}{% endcapture %} 

```

This is the result :  
 ![03](https://canada1.discourse-cdn.com/flex030/uploads/getsilverfin/original/1X/d547208360b636f1fc15e4e3f4c1faf1b189f84c.png)

---

<div class="post-metadata">

**Author:** ![Bart\_Verhaeghe](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Bart\_Verhaeghe](https://community.silverfin.com/u/Bart_Verhaeghe)\
**Post date:** [April 5, 2018, 1:08pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802/2 "2018-04-05T13:08:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sven](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/sven/32/1495_2.png) [@sven](https://community.silverfin.com/u/sven)\
**Post date:** [April 5, 2018, 1:09pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802/3 "2018-04-05T13:09:59Z")

</div>

Indeed I am @Bart_Verhaeghe ⛹️‍♂️

---

<div class="post-metadata">

**Author:** ![Bart\_Verhaeghe](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Bart\_Verhaeghe](https://community.silverfin.com/u/Bart_Verhaeghe)\
**Post date:** [April 5, 2018, 1:11pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802/4 "2018-04-05T13:11:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bart\_Verhaeghe](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Bart\_Verhaeghe](https://community.silverfin.com/u/Bart_Verhaeghe)\
**Post date:** [September 24, 2018, 4:38pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802/5 "2018-09-24T16:38:55Z")

</div>

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?

Thanks!

---

<div class="post-metadata">

**Author:** ![sven](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/sven/32/1495_2.png) [@sven](https://community.silverfin.com/u/sven)\
**Post date:** [October 1, 2018, 12:22pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802/6 "2018-10-01T12:22:30Z")

</div>

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 😉  
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)?

---

<div class="post-metadata">

**Author:** ![SamVanEenoo](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/samvaneenoo/32/134_2.png) [@SamVanEenoo](https://community.silverfin.com/u/SamVanEenoo)\
**Post date:** [November 28, 2018, 12:53pm UTC](https://community.silverfin.com/t/case-how-to-create-a-fori-loop-within-a-fori-loop/802/7 "2018-11-28T12:53:03Z")

</div>

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 item.name %}
> {% endfori %}
> {% endfori %}
> 
> ```

Kind regards  
Sam
