If / else
The if
property is a conditional structure that allows us to perform checks on specific system variables. With it, we can evaluate whether a given condition is true or false and execute different blocks of code based on that evaluation.
For example, check if the order.total_price
variable is greater than 150 and dynamically apply a discount coupon based on the value:
In this example, we check whether the order.total_price
variable is greater than 150 using the {% if %}
directive. If true, a 15% discount is applied, displaying the new discounted and formatted value. Otherwise, the {% else %}
directive is used to apply a 10% discount, also showing the updated and formatted value.
In both cases, the code displays the current order value and the discounted value, along with an indication of which discount was applied.
Sample output from the code: