Conditions
Conditions let you show or hide content based on your data. They work like if statements in any programming language.
Basic if statement
{% if is_paid %}
<p class="status">Paid</p>
{% endif %}
This only renders the paragraph if is_paid is true.
If / else
{% if is_paid %}
<p class="status paid">Paid</p>
{% else %}
<p class="status unpaid">Unpaid</p>
{% endif %}
If / elif / else
For multiple conditions, use elif:
{
"product": "Apple Juice",
"life_span": 180
}
{% if life_span > 160 and life_span < 180 %}
<p>{{product}} is expiring soon!</p>
{% elif life_span >= 180 %}
<p>{{product}} has expired!</p>
{% else %}
<p>It's perfect</p>
{% endif %}
Output:
Apple Juice has expired!
Comparison operators
| Operator | Description | Example |
|---|---|---|
== | Equal to | val1 == val2 |
!= | Not equal to | val1 != val2 |
> | Greater than | val1 > val2 |
>= | Greater than or equal | val1 >= val2 |
< | Less than | val1 < val2 |
<= | Less than or equal | val1 <= val2 |
Comparison functions
APITemplate.io also supports comparison functions as an alternative:
| Function | Description | Example |
|---|---|---|
gt | Greater than | gt(val1, val2) |
gte | Greater than or equal | gte(val1, val2) |
lt | Less than | lt(val1, val2) |
lte | Less than or equal | lte(val1, val2) |
{% if gt(life_span, 160) and lt(life_span, 180) %}
<p>Expiring soon!</p>
{% elif gte(life_span, 180) %}
<p>Expired!</p>
{% endif %}
Logical operators
| Operator | Description |
|---|---|
and | True if both conditions are true |
or | True if either condition is true |
not | Inverts the result |
{% if quantity > 0 and is_available %}
<p>In stock</p>
{% endif %}
{% if not is_archived %}
<p>This document is active</p>
{% endif %}
Checking if a variable exists
{% if notes %}
<div class="notes">{{notes}}</div>
{% endif %}
An {% if %} block evaluates to true if the variable exists, is not empty, and is not false.