CASE: how to use a fori-loop?

Pretty important case, because our templates are full of fori-loops.

First though: what is a fori loop?

Let’s say you want to input a list of all the investments you’ve done. If you create following variable :

{% input %}

you’ll just be able to input 1 sort of investment, right? Makes sense, because it’s one variable also.

But, as you know is the case in Silverfin: if you put in one thing, another line appears under it. That function is created by a fori-loop. And it means you can put in data, line after line (input after input). And all that data will be stored in a so-called ‘collection’: think of it as a little database, with all variables in it.

So how do we create a collection, that we will fill with a fori-loop?

Choose the name of your collection

Let’s say

Notice that we only use the_namespace part of our custom variable. We are going to use the_key part somewhere else. ( see also this for more info about the_namespace and the_key)

Create fori-loop

This is how you create a fori-loop :

{% fori x in collection %}
        {% input x.something %}
{% endfori %}

with x being the link between your collection and the variables you create in it. The x is basically the_key part in this.

In our example, we can put this :

{% fori inv in %}
     {% input inv.soort %}
{% endfori %}

So : is our little database that’s gonna be filled with variables.
soort is the name of that variable
inv is the link between the collection and the variables. You can call this however you like it though…

The code between a fori-loop will be executed every time you put in new data.

So, if you give in iPhone, and then in another input-line Macbook, you fill the variable ‘soort’ in our collection ‘’ with 2 inputs.

How to show data stored in a collection?

To show the stored data, you use a for-loop.

Just the same as a fori-loop, but then like this:

{% for inv in %}
     {{ inv.soort }}
{% endfor %}

You don’t even have to use ‘inv’ but can choose something else, like :

{% for investering in %}
     {{ investering.soort }}
{% endfor %}

The code between the {% for %} and {% endfor %} will be executed as much as data is found. So if we have iPhone & Macbook in it, the iteration of the forloop will be done twice.