Forloop with if-statement table layout

The layout of a table is lost with following code:

Code :

|Wagen|Nummerplaat|Chassis|1e inschrijving|CO2|Fiscaal|Out op {% for wagenpark in company.custom.wagenpark %}{% capture wagenin %}{{ wagenpark.in-0 }}{% endcapture %}{% capture wagenout %}{{ wagenpark.out-0 }}{% endcapture %}{% if wagenout == "0.0" or wagenout >= beginboekjaar %}
|:-----|---:|---:|---:|---:|----:|----:|---:|
{% stripnewlines %}
|{{ wagenpark.wagen}}
|{{ wagenpark.nummerplaat }}
|{{ wagenpark.chassisnummer }}
|{{ wagenpark.eerste_inschrijving }}
|{{ wagenpark.co2 }}
|{% if wagenpark.brandstof == "Diesel" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 116 %}80%{% endif %}
    {% if wagenpark.co2 > 115 and wagenpark.co2 < 146 %}75%{% endif %}
    {% if wagenpark.co2 > 145 and wagenpark.co2 < 171 %}70%{% endif %}
    {% if wagenpark.co2 > 170 and wagenpark.co2 < 196 %}60%{% endif %}
    {% if wagenpark.co2 > 195 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Benzine" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 126 %}80%{% endif %}
    {% if wagenpark.co2 > 125 and wagenpark.co2 < 156 %}75%{% endif %}
    {% if wagenpark.co2 > 155 and wagenpark.co2 < 181 %}70%{% endif %}
    {% if wagenpark.co2 > 180 and wagenpark.co2 < 206 %}60%{% endif %}
    {% if wagenpark.co2 > 205 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Elektrisch" %}
120%
{% endif %}
|{{ wagenpark.out }}{% endstripnewlines %}
{% endif %}

{% endfor %}

image

Hello @Thijs,

I’d suggest putting the whole code between stripnewlines and for each newline in the table, adding the tag {% newline %} to it.

Like this for instance :

{% stripnewlines %}
|Wagen|Nummerplaat|Chassis|1e inschrijving|CO2|Fiscaal|Out op {% for wagenpark in company.custom.wagenpark %}{% capture wagenin %}{{ wagenpark.in-0 }}{% endcapture %}{% capture wagenout %}{{ wagenpark.out-0 }}{% endcapture %}{% if wagenout == "0.0" or wagenout >= beginboekjaar %}
{% if forloop.first %}
{% newline %}
|:-----|---:|---:|---:|---:|----:|----:|---:|
{% endif %}
{% newline %}
|{{ wagenpark.wagen}}
|{{ wagenpark.nummerplaat }}
|{{ wagenpark.chassisnummer }}
|{{ wagenpark.eerste_inschrijving }}
|{{ wagenpark.co2 }}
|{% if wagenpark.brandstof == "Diesel" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 116 %}80%{% endif %}
    {% if wagenpark.co2 > 115 and wagenpark.co2 < 146 %}75%{% endif %}
    {% if wagenpark.co2 > 145 and wagenpark.co2 < 171 %}70%{% endif %}
    {% if wagenpark.co2 > 170 and wagenpark.co2 < 196 %}60%{% endif %}
    {% if wagenpark.co2 > 195 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Benzine" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 126 %}80%{% endif %}
    {% if wagenpark.co2 > 125 and wagenpark.co2 < 156 %}75%{% endif %}
    {% if wagenpark.co2 > 155 and wagenpark.co2 < 181 %}70%{% endif %}
    {% if wagenpark.co2 > 180 and wagenpark.co2 < 206 %}60%{% endif %}
    {% if wagenpark.co2 > 205 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Elektrisch" %}
120%
{% endif %}
|{{ wagenpark.out }}
{% endif %}
{% endfor %}
{% endstripnewlines %} 

More info on newlines here :

Dear @sven

Thanks!

One prolblem. If the if statement returns false at the first line, the lay out breaks:

Breaking @Thijs? Or did you mean that the headers are still visible even if there’s no data in your custom collection?

The better code is this :

{% stripnewlines %}
{% for wagenpark in company.custom.wagenpark %}
{% if forloop.first %}
| Wagen
| Nummerplaat
| Chassis
| 1e inschrijving
| CO2
| Fiscaal
| Out op {% capture wagenin %}{{ wagenpark.in-0 }}{% endcapture %}{% capture wagenout %}{{ wagenpark.out-0 }}{% endcapture %}{% if wagenout == "0.0" or wagenout >= beginboekjaar %}
{% newline %}
|:-----|---:|---:|---:|---:|----:|----:|---:|
{% endif %}
{% newline %}
|{{ wagenpark.wagen}}
|{{ wagenpark.nummerplaat }}
|{{ wagenpark.chassisnummer }}
|{{ wagenpark.eerste_inschrijving }}
|{{ wagenpark.co2 }}
|{% if wagenpark.brandstof == "Diesel" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 116 %}80%{% endif %}
    {% if wagenpark.co2 > 115 and wagenpark.co2 < 146 %}75%{% endif %}
    {% if wagenpark.co2 > 145 and wagenpark.co2 < 171 %}70%{% endif %}
    {% if wagenpark.co2 > 170 and wagenpark.co2 < 196 %}60%{% endif %}
    {% if wagenpark.co2 > 195 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Benzine" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 126 %}80%{% endif %}
    {% if wagenpark.co2 > 125 and wagenpark.co2 < 156 %}75%{% endif %}
    {% if wagenpark.co2 > 155 and wagenpark.co2 < 181 %}70%{% endif %}
    {% if wagenpark.co2 > 180 and wagenpark.co2 < 206 %}60%{% endif %}
    {% if wagenpark.co2 > 205 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Elektrisch" %}
120%
{% endif %}
|{{ wagenpark.out }}
{% endif %}
{% endfor %}
{% endstripnewlines %}

So the headers are only displayed when there’s data in your collection. Other than that, I don’t see issues with this code.

If you have, then perhaps you’ll need to post your code again I’m afraid.

@sven

I meant that the headers are not bold if the first line of my fori-loop is not included in my forloop due the if-statement-> as you can see in my previous post.

Ah okay… But that’s fixed with the last code by having the complete header in the forloop.first @Thijs.

this is okay for you?

@sven

With your code i get this: (with only the first one)

Can you post your code, as well perhaps the code you use to fill your collection? @Thijs

Because tested this already and it works for me; not sure why your table gets broken

{% capture Bboekjaar %}{{ period.year_start_date | date:'%d/%m/%Y' }}{% endcapture %}{% capture beginboekjaar %}{{ Bboekjaar-0 }}{% endcapture %}
{% capture Eboekjaar %}{{ period.year_end_date | date:'%d/%m/%Y' }}{% endcapture %}{% capture eindeboekjaar %}{{ Eboekjaar-0 }}{% endcapture %}

{% stripnewlines %}
{% for wagenpark in company.custom.wagenpark %}
{% if forloop.first %}
| Wagen
| Nummerplaat
| Chassis
| 1e inschrijving
| CO2
| Fiscaal
| Out op {% capture wagenin %}{{ wagenpark.in-0 }}{% endcapture %}{% capture wagenout %}{{ wagenpark.out-0 }}{% endcapture %}{% if wagenout == "0.0" or wagenout >= beginboekjaar %}
{% newline %}
|:-----|---:|---:|---:|---:|----:|----:|---:|
{% endif %}
{% newline %}
|{{ wagenpark.wagen}}
|{{ wagenpark.nummerplaat }}
|{{ wagenpark.chassisnummer }}
|{{ wagenpark.eerste_inschrijving }}
|{{ wagenpark.co2 }}
|{% if wagenpark.brandstof == "Diesel" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 116 %}80%{% endif %}
    {% if wagenpark.co2 > 115 and wagenpark.co2 < 146 %}75%{% endif %}
    {% if wagenpark.co2 > 145 and wagenpark.co2 < 171 %}70%{% endif %}
    {% if wagenpark.co2 > 170 and wagenpark.co2 < 196 %}60%{% endif %}
    {% if wagenpark.co2 > 195 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Benzine" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 126 %}80%{% endif %}
    {% if wagenpark.co2 > 125 and wagenpark.co2 < 156 %}75%{% endif %}
    {% if wagenpark.co2 > 155 and wagenpark.co2 < 181 %}70%{% endif %}
    {% if wagenpark.co2 > 180 and wagenpark.co2 < 206 %}60%{% endif %}
    {% if wagenpark.co2 > 205 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Elektrisch" %}
120%
{% endif %}
|{{ wagenpark.out }}
{% endif %}
{% endfor %}
{% endstripnewlines %}
<b><b>


|----|----|--20%--|----|----|----|----|----|----|----|---|---|---|---+{% fori wagenpark in company.custom.wagenpark %}
{% stripnewlines %}
|{% input wagenpark.wagen %}
|{% input wagenpark.nummerplaat %}
|{% input wagenpark.chassisnummer %}
|{% input wagenpark.soort as:select options:"Aankoop|Leasing|Renting" %}
|{% input wagenpark.aankoopfactuur as:file %}
|{% input wagenpark.contract as:file %}
|{% input wagenpark.co2 %}
|{% input wagenpark.cataloguswaarde as:currency %}
|{% input wagenpark.brandstof as:select options:"Diesel|Benzine|Elektrisch"%}
|{% input wagenpark.eerste_inschrijving as:date %}
|{% input wagenpark.btw_aftrekbaar as:percentage %}
|{% input wagenpark.in as:date %}
|{% input wagenpark.out as:date %}{% endstripnewlines %}{% endfori %}

It’s was this little thing right here that broke your table :

{% if wagenpark.brandstof == "Elektrisch" %}
120%
{% endif %}
|{{ wagenpark.out }}
{% endif %}
{% endfor %}
{% endstripnewlines %}
<b><b> 

That

never should be after a table; before and after a table always leave an empty line.

Should be good now like this:

{% if wagenpark.brandstof == "Elektrisch" %}
120%
{% endif %}
|{{ wagenpark.out }}
{% endif %}
{% endfor %}
{% endstripnewlines %}

<b><b> 

I’m affraid it didn’t solve it.

First, I think the

{% if wagenout == “0.0” or wagenout >= beginboekjaar %}

is not in the right place anymore, because I get only the first line of the loop (it was changed with the stripnewlines):

Second, when i add a date to the first line in my foriloop:

image

It loses the layout and I should not by displayed in the forloop, because the date is 31/12/2016 ( {% if wagenout == "0.0" or wagenout >= beginboekjaar %} )

Many thanks!

Overlooked that obviously, my bad…

this should fix it, because the capture are now out of the if forloop.first-statement as well putting the if-statement after the table :

{% capture Bboekjaar %}{{ period.year_start_date | date:'%d/%m/%Y' }}{% endcapture %}{% capture beginboekjaar %}{{ Bboekjaar-0 }}{% endcapture %}
{% capture Eboekjaar %}{{ period.year_end_date | date:'%d/%m/%Y' }}{% endcapture %}{% capture eindeboekjaar %}{{ Eboekjaar-0 }}{% endcapture %}

{% stripnewlines %}
{% for wagenpark in company.custom.wagenpark %}
{% if forloop.first %}
| Wagen
| Nummerplaat
| Chassis
| 1e inschrijving
| CO2
| Fiscaal
| Out op 
{% newline %}
|:-----|---:|---:|---:|---:|----:|----:|---:|
{% endif %}
{% capture wagenin %}{{ wagenpark.in-0 }}{% endcapture %}
{% capture wagenout %}{{ wagenpark.out-0 }}{% endcapture %}
{% if wagenout == "0.0" or wagenout >= beginboekjaar %}
{% newline %}
|{{ wagenpark.wagen}}
|{{ wagenpark.nummerplaat }}
|{{ wagenpark.chassisnummer }}
|{{ wagenpark.eerste_inschrijving }}
|{{ wagenpark.co2 }}
|{% if wagenpark.brandstof == "Diesel" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 116 %}80%{% endif %}
    {% if wagenpark.co2 > 115 and wagenpark.co2 < 146 %}75%{% endif %}
    {% if wagenpark.co2 > 145 and wagenpark.co2 < 171 %}70%{% endif %}
    {% if wagenpark.co2 > 170 and wagenpark.co2 < 196 %}60%{% endif %}
    {% if wagenpark.co2 > 195 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Benzine" %}
    {% if wagenpark.co2 < 61 %}100%{% endif %}
    {% if wagenpark.co2 > 60 and wagenpark.co2 < 106 %}90%{% endif %}
    {% if wagenpark.co2 > 105 and wagenpark.co2 < 126 %}80%{% endif %}
    {% if wagenpark.co2 > 125 and wagenpark.co2 < 156 %}75%{% endif %}
    {% if wagenpark.co2 > 155 and wagenpark.co2 < 181 %}70%{% endif %}
    {% if wagenpark.co2 > 180 and wagenpark.co2 < 206 %}60%{% endif %}
    {% if wagenpark.co2 > 205 %}50%{% endif %}
{% endif %}

{% if wagenpark.brandstof == "Elektrisch" %}
120%
{% endif %}
|{{ wagenpark.out }}
{% endif %}
{% endfor %}
{% endstripnewlines %}

<b><b>


|----|----|--20%--|----|----|----|----|----|----|----|---|---|---|---+{% fori wagenpark in company.custom.wagenpark %}
{% stripnewlines %}
|{% input wagenpark.wagen %}
|{% input wagenpark.nummerplaat %}
|{% input wagenpark.chassisnummer %}
|{% input wagenpark.soort as:select options:"Aankoop|Leasing|Renting" %}
|{% input wagenpark.aankoopfactuur as:file %}
|{% input wagenpark.contract as:file %}
|{% input wagenpark.co2 %}
|{% input wagenpark.cataloguswaarde as:currency %}
|{% input wagenpark.brandstof as:select options:"Diesel|Benzine|Elektrisch"%}
|{% input wagenpark.eerste_inschrijving as:date %}
|{% input wagenpark.btw_aftrekbaar as:percentage %}
|{% input wagenpark.in as:date %}
|{% input wagenpark.out as:date %}{% endstripnewlines %}{% endfori %} 

No issue with dates however

Worked perfect! :smile:

Many thanks!

Glad to help; nice looking template :ok_hand:

Work in progress :slightly_smiling_face: