# Nested fori-loops

**URL:** <https://community.silverfin.com/t/nested-fori-loops/380>\
**Category:** Templates\
**Tags:** iterations, fori\_loop\
**Created:** [August 30, 2017, 10:58am UTC](https://community.silverfin.com/t/nested-fori-loops/380 "2017-08-30T10:58:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DirkTim](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@DirkTim](https://community.silverfin.com/u/DirkTim)\
**Post date:** [August 30, 2017, 10:58am UTC](https://community.silverfin.com/t/nested-fori-loops/380/1 "2017-08-30T10:58:42Z")

</div>

Hello,

I’m working on a new template which requires two “fori” loops nested within one other. What I need is something like a “Loans and leasings” template but where I can add multiple loans on the same account.

1. The first loop is used to create mutliple loans.  
For that I use:

2. The second loop is located inside the first one and is used to create a table with the refunds, date, remaining capital, etc… For that I use:

The problem with that code is that if I fill the table once, it stays the same for every new loan. And if I change the values of one loan, it changes for every other one.

From what I understand I should link the table to the current detail so that every new loan has its own table.

Do you know a fix to this problem?

Thank you,

Jean-François on the PC of Dirk Timmermans

---

<div class="post-metadata">

**Author:** ![Mathias](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/mathias/32/125_2.png) [@Mathias](https://community.silverfin.com/u/Mathias)\
**Post date:** [August 31, 2017, 9:59am UTC](https://community.silverfin.com/t/nested-fori-loops/380/2 "2017-08-31T09:59:01Z")

</div>

Hello @DirkTim,

You can use a key in the second fori loop.

So instead of doing:

```auto
{% assign items = custom.items %}
{% fori item in items %} <— my table --> {% endfori %}

```

You could use (for example):

`{% fori item in custom[loan.key] %}`

This code attributes a key to each loan (in the example). We work like this in the ‘Deelnemingen’ account template. You could apply the same way of working.

Please let us know if this would not work for you or if you need our help. Could you then please post the piece of the code of the fori loops, or tell us the name of the template? So we can take a look at the full code in your collection.

Thanks in advance

Mathias

---

<div class="post-metadata">

**Author:** ![Wim](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@Wim](https://community.silverfin.com/u/Wim)\
**Post date:** [November 23, 2017, 12:43pm UTC](https://community.silverfin.com/t/nested-fori-loops/380/3 "2017-11-23T12:43:15Z")

</div>

Hi,

I building a template for Gespreide taxatie. where i have multiple Meerwaardes with multiple Wederbelegginen and Onttrekkingen.

i’m stuck at the 3 fori. i’m getting following error: Liquid error: Custom values can only go two levels deep.

Here my 3 fori’s:

```
{%fori meerwaarde in custom.meerwaardes %}
{% fori wederbeleggingid in custom[meerwaarde.key] %}
{% fori onttrekking in custom[meerwaarde.key] %}

```

The 2nd and 3th fori are in the same “custom[meerwaarde.key]” i tried changing it but doesn’t seem to work.

Any help?

---

<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 23, 2017, 1:21pm UTC](https://community.silverfin.com/t/nested-fori-loops/380/4 "2017-11-23T13:21:56Z")

</div>

Hi Wim

Try this:

```
{% fori meerwaarde in custom.meerwaardes %}
{% fori wederbeleggingid in custom[meerwaarde.key] %}
{% fori onttrekking in custom[wederbeleggingid.key] %}

```

This should work.

Kind regards  
Sam

---

<div class="post-metadata">

**Author:** ![Wim](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@Wim](https://community.silverfin.com/u/Wim)\
**Post date:** [November 23, 2017, 1:53pm UTC](https://community.silverfin.com/t/nested-fori-loops/380/5 "2017-11-23T13:53:17Z")

</div>

I’ve adjusted the code like you suggested but it’s not working.

getting this error in the last fori `{% fori onttrekking in custom[wederbeleggingid.key] %}`: Liquid error: undefined method `split’ for nil:NilClass

---

<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 23, 2017, 2:02pm UTC](https://community.silverfin.com/t/nested-fori-loops/380/6 "2017-11-23T14:02:11Z")

</div>

Hi Wim

The code beneath works fine without any errors… The error must be made somewhere else or for another reason.

```
{% fori meerwaarde in custom.meerwaardes %}
  {% input meerwaarde.thing %}
  {% fori wederbeleggingid in custom[meerwaarde.key] %}
    {% input wederbeleggingid.thing %}
    {% fori onttrekking in custom[wederbeleggingid.key] %}
      {% input onttrekking.thing %}
    {% endfori %}
  {% endfori %}
{% endfori %}

```

Could you post the entire code so I can have a look?

---

<div class="post-metadata">

**Author:** ![Wim](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@Wim](https://community.silverfin.com/u/Wim)\
**Post date:** [November 23, 2017, 2:08pm UTC](https://community.silverfin.com/t/nested-fori-loops/380/7 "2017-11-23T14:08:42Z")

</div>

```
{% fori meerwaarde in custom.meerwaardes %} {% assign $2 = 0 %}{% assign totaal_ontrekkinguitgesteldebelasting = 0 %}{% assign totaal_ontrekkingreserve = 0 %}
**_{% input meerwaarde.omschrijving placeholder:'geef hier omschrijving'%}_**
{% input meerwaarde.omschrijving_info as:text size:mini placeholder:'meer info'%}

|--------|:-------------------------------------------------|-------------------------------------------------------------------------------:|-------:|-----:+
|Verkoopprijs |{% input meerwaarde.verkoopdatum as:date %}: | {% input meerwaarde.verkoopprijs as:currency %}{% assign verkoopprijs = meerwaarde.verkoopprijs %} |
|Boekwaarde |{% input meerwaarde.aankoopdatum as:date %}: | {% input meerwaarde.aankoopprijs as:n_currency %}{% assign aankoopprijs = meerwaarde.aankoopprijs %}{% assign meerwaardebedrag = (meerwaarde.verkoopprijs+meerwaarde.aankoopprijs) %}||
| **Meerwaarde gespreide taxatie** | | **{{meerwaardebedrag | currency}}** | **x {%$1+input meerwaarde.perc as:select options:'24,98%|30,00%|33,99%|70,00%|75,02%|66,01%' option_values:'0.2498|0.3000|0.3399|0.7000|0.7502|0.6601' %} =** | {% assign gespreidemw = (meerwaardebedrag*$1) %} **{{gespreidemw |currency}}** |
| **Uitgestelde belasting** | | **{{meerwaardebedrag | currency}}** | **x {% assign percentage_ub = (1-$1) %} {{ percentage_ub | percentage }} =** | {% assign gespreide_ub = (meerwaardebedrag*percentage_ub) %} **{{gespreide_ub |currency}}** |
|{% assign $1 = 0 %}

**_Wederbeleggingen:_**

|Omschrijving | Bedrag |
|-------80%---|---------------------20%----------------------:{% fori wederbeleggingid in custom[meerwaarde.key] %}
|{% input wederbeleggingid.wederbelegging_omschrijving as:text size:mini placeholder:'Omschrijf de wederbelegging'%}| {%$2+input wederbeleggingid.wederbelegging_waarde as:currency placeholder:'Bedrag investering' %}{% if forloop.last %}{% assign wederbelegging = $2 %}
| **Totaal** | **{{wederbelegging |currency}}** {%endif%}{% endfori %}

{% assign voorwaarde =($2-verkoopprijs) %}

{% if voorwaarde >= 0 %}
{% ic %}

**<font color="#FF0000" size="5">Opgelet!</font>**

Het bedrag van de wederbelegging is groter dan de verkoopprijs.  
Onttrekkingen aanpassen! 
Maximaal op te nemen waarde = {{ verkoopprijs | currency }} {% assign wederbelegging = verkoopprijs %}
{% endic %}{%endif%}

**_Ontrekkingen:_**

|Boekjaar | Onttrekking | | Onttrekking reserve | Onttrekking uitg. belasting |
|--20%------------------------|-----10%-----|----40%-----------|-------------------------:|-----------------------------:{% fori onttrekking in custom[wederbeleggingid.key] %}
|{% input onttrekking.date as:date %}{% stripnewlines %}
|Reserve <br> Uitg. belasting
|{{ gespreidemw | currency }} x {% input onttrekking.afschrijving as:currency default:'0' %} / {% input onttrekking.wederbelegging as:currency placeholder_default:wederbelegging %} 
<br>
{{ gespreide_ub | currency }} x {{onttrekking.afschrijving |currency}} / {{ onttrekking.wederbelegging |currency }}
|{% assign ontrekkingreserve = (gespreidemw*onttrekking.afschrijving/onttrekking.wederbelegging) %} {{ ontrekkingreserve | currency }}
{% unless forloop.last %}{% assign totaal_ontrekkingreserve = totaal_ontrekkingreserve+ontrekkingreserve %}
{% comment %}Bereken totaal van alles onttrekkingen reserves!{% endcomment %}{% assign totaalrek_ontrekkingreserve = totaal_ontrekkingreserve+ontrekkingreserve %}{% endunless %}
|<br>{% assign ontrekkinguitgesteldebelasting = (gespreide_ub*onttrekking.afschrijving/onttrekking.wederbelegging) %} {{ ontrekkinguitgesteldebelasting | currency }}
{% unless forloop.last %}{% assign totaal_ontrekkinguitgesteldebelasting = totaal_ontrekkinguitgesteldebelasting+ontrekkinguitgesteldebelasting %}
{% comment %}Bereken totaal van alles onttrekkingen UB!{% endcomment %}{% assign totaalrek_ontrekkinguitgesteldebelasting = totaal_ontrekkinguitgesteldebelasting+ontrekkinguitgesteldebelasting %}{% endunless %}
{% endstripnewlines %}{% if forloop.last %}
| **Totaal onttrekkingen** ||| **{{ totaal_ontrekkingreserve |currency }}** | **{{ totaal_ontrekkinguitgesteldebelasting |currency }}** {% endif %}{% endfori %}

<br>

{% endfori %}
```

---

<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 23, 2017, 3:57pm UTC](https://community.silverfin.com/t/nested-fori-loops/380/8 "2017-11-23T15:57:41Z")

</div>

Now I understand what went wrong. I thought you went 3 levels deep, but you only went 2 levels deep but are using 2 foriloops nested in another foriloop.

The problem here is that every custom variable needs to be unique.

Your original code looked something like this

```
{% fori meerwaarde in custom.meerwaardes %}
  {% fori wederbeleggingid in custom[meerwaarde.key] %}
     ....
  {% endfori %}

  {% fori onttrekking in custom[meerwaarde.key] %}
     ....
  {% endfori %}
{% endfori %}

```

The problem with this is that you are making 2 variable with the same name, because you are using meerwaarde.key twice. You need to make these keys unique. You can do this the following way:

```
{% capture wederbeleggingid_key = %}wederbeleggingid_{{ meerwaarde.key }}{% endcapture %}
{% fori wederbeleggingid in custom[wederbeleggingid_key] %}

```

You can do the same for onttrekkingen.
