Nested fori-loops

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:

    {% fori detail in current_account.details %} <— my loan → {% endfori %}

  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:

    {% assign items = custom.items %}
    {% fori item in items %} <— my table → {% endfori %}

    NB: I need to have something like item.name, item.date, etc…

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

Hello @DirkTim,

You can use a key in the second fori loop.

So instead of doing:

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

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?

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

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

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?

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

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.

1 Like