Skip to main content

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

OperatorDescriptionExample
==Equal toval1 == val2
!=Not equal toval1 != val2
>Greater thanval1 > val2
>=Greater than or equalval1 >= val2
<Less thanval1 < val2
<=Less than or equalval1 <= val2

Comparison functions

APITemplate.io also supports comparison functions as an alternative:

FunctionDescriptionExample
gtGreater thangt(val1, val2)
gteGreater than or equalgte(val1, val2)
ltLess thanlt(val1, val2)
lteLess than or equallte(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

OperatorDescription
andTrue if both conditions are true
orTrue if either condition is true
notInverts 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.

Next steps

  • Loops — repeat content for lists and arrays
  • Filters — transform values