I am trying to use a Twig (version 2.x) if/elseif/else statement to display some text based on the balance of an order, but it seems that Twig is parsing a negative integer as a positive one.
{% if (order.balance) > 0 %}
You have EUR {{ order.open_amount }} left to pay!
{% elseif (order.balance) < 0 %}
You have paid to much, the remainder will be deposited into your account.
{% else %}
You have paid, thank you.
{% endif %}
order.balance is fed into Twig as an integer and can be either negative, 0 or positive.
The piece of code however has the following result:
order.balance = 0
Expected: You have paid, thank you.
Actual: You have paid, thank you.
order.balance = 29999
Expected: You have EUR 299,99 left to pay!
Actual: You have EUR 299,99 left to pay!
order.balance = -10001
Expected: You have paid to much, the remainder will be deposited into your account.
Actual: You have EUR -100,01 left to pay!
The logic I am following is more or less as described in an example here: https://twig.symfony.com/doc/2.x/tags/if.html
What am I missing here?
You have just inverted your condition or the sentence you want to display, or your logic, even.
{% if order.balance < 0 -%} {#- that condition was > 0 -#}
You have EUR {{ order.open_amount }} left to pay!
{% elseif order.balance > 0 -%} {#- and this one was < 0 -#}
You have paid to much, the remainder will be deposited into your account.
{% else -%}
You have paid, thank you.
{% endif %}
Here is a fiddle representing this: https://twigfiddle.com/r3cdr8
Answer:
I tried putting in the 3 different options as 3 different IF statements and then things work out fine, better to avoid if/elseif/else structures where ever possible with Twig?
{% if order.balance > 0 %}You have EUR {{ order.open_amount }} left to pay!{% endif %}
{% if order.balance < 0 %}You have paid to much, the remainder will be deposited into your account.{% endif %}
{% if order.balance == 0 %}You have paid, thank you.{% endif %}
Do it like this and it works perfectly.