Let’s say we have following template :
with this code :
*Select accounts here* {% input period.custom.accounts.costs as:account_collection range:61 accounts_var:costs %}
<br>
{% stripnewlines %}
{% newline %}
| Number | Name | Supplier | Remark
{% newline %}
|---10%-----------|---40%--------|----25%------------|-----25%-----------+
{% for acc in costs %}
{% newline %}
|{{ acc.number }}
|{{ acc.name }}
|{% input acc.custom.costs.supplier %}
|{% input acc.custom.costs.remark %}
{% endfor %}
{% endstripnewlines %}
But what if we would like to give in multiple suppliers for each account? Because now with this code you can give only 1 input, and not several.
Create extra fori-loop in each loop
For each iteration of a loop, we’re gonna need to create a new fori-loop to input multiple suppliers. And we have to ask ourselves, what makes each loop unique?
The answer is a variable in each loop: the accountnumber.
If we create a collection that is “attached” to that accountnumber, we can create input-variable with a fori-loop:
{% newline %}
|{{ acc.number }}
|{{ acc.name }}
|
{% fori supp in custom[acc.number] %}
{% input supp.name %}
{% endfori %}
So, the custom[acc.number] is our collection that is unique for each loop, because it’s attached to the object acc.number
. Because the acc.number
is a variable, we use [ ] to create that collection.
However, you may see something like this now :
And we need our input to be on a new line for each input, so we’re use the break-tag
for that:
{% newline %}
|{{ acc.number }}
|{{ acc.name }}
|
{% fori supp in custom[acc.number] %}
<br>{% input supp.name %}
{% endfori %}
|{% input acc.custom.costs.remark %}
That break-tag however, can’t be executed or it will result in this :
So only in the first iteration of the loops, the break tag can’t be used. We can do this with a forloop.first and an unless-statement:
{% newline %}
|{{ acc.number }}
|{{ acc.name }}
|
{% fori supp in custom[acc.number] %}
{% unless forloop.first %}
<br>
{% endunless %}
{% input supp.name %}
{% endfori %}
|{% input acc.custom.costs.remark %}
This will give the result we need :
PS for more info about forloop.first, check this topic about it.