Skip to content

Built-in Tests (28)

Tests check values using the is operator (Jinja2 syntax).

Usage

{% if value is defined %}...{% endif %}
{% if num is even %}...{% endif %}
{% if num is divisibleby(3) %}...{% endif %}

Negation

Use is not for negative tests:

{% if value is not none %}...{% endif %}
{% if num is not even %}...{% endif %}

Available Tests

Type Tests

TestDescriptionExample
definedVariable exists{% if x is defined %}
undefinedVariable doesn’t exist{% if x is undefined %}
noneValue is null/undefined{% if x is none %}
booleanValue is boolean{% if x is boolean %}
stringValue is string{% if x is string %}
numberValue is number{% if x is number %}
integerValue is integer{% if x is integer %}
floatValue is float{% if x is float %}
mappingValue is object/dict{% if x is mapping %}
iterableValue is iterable{% if x is iterable %}
sequenceValue is array{% if x is sequence %}
callableValue is function{% if x is callable %}

Number Tests

TestDescriptionExample
evenNumber is even{% if n is even %}
oddNumber is odd{% if n is odd %}
divisibleby(n)Divisible by n{% if n is divisibleby(3) %}

Comparison Tests

TestDescriptionExample
eq(value)Equal to{% if x is eq(5) %}
ne(value)Not equal to{% if x is ne(5) %}
lt(value)Less than{% if x is lt(10) %}
le(value)Less than or equal{% if x is le(10) %}
gt(value)Greater than{% if x is gt(0) %}
ge(value)Greater than or equal{% if x is ge(0) %}
sameas(value)Same object reference{% if x is sameas(y) %}

String Tests

TestDescriptionExample
upperString is uppercase{% if s is upper %}
lowerString is lowercase{% if s is lower %}

Collection Tests

TestDescriptionExample
emptyCollection is empty{% if items is empty %}
in(collection)Value in collection{% if x is in(items) %}

Boolean Tests

TestDescriptionExample
trueValue is true{% if x is true %}
falseValue is false{% if x is false %}
truthyValue is truthy{% if x is truthy %}
falsyValue is falsy{% if x is falsy %}

Examples

Check if variable exists

{% if user is defined %}
Hello, {{ user.name }}!
{% else %}
Hello, Guest!
{% endif %}

Alternate row colors

{% for item in items %}
<tr class="{% if loop.index is even %}even{% else %}odd{% endif %}">
<td>{{ item.name }}</td>
</tr>
{% endfor %}

Group items by divisibility

{% for n in numbers %}
{% if n is divisibleby(3) %}
<span class="divisible-3">{{ n }}</span>
{% elif n is divisibleby(2) %}
<span class="divisible-2">{{ n }}</span>
{% else %}
<span>{{ n }}</span>
{% endif %}
{% endfor %}

Handle empty collections

{% if items is empty %}
<p>No items found.</p>
{% else %}
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endif %}

Type checking

{% if value is string %}
String: {{ value }}
{% elif value is number %}
Number: {{ value|floatformat:2 }}
{% elif value is sequence %}
List: {{ value|join:", " }}
{% else %}
Other: {{ value }}
{% endif %}

Programmatic Access

import { builtinTests } from 'binja'
// All 28 built-in tests
console.log(Object.keys(builtinTests))
// ['divisibleby', 'even', 'odd', 'number', 'integer', 'float',
// 'defined', 'undefined', 'none', 'boolean', 'string', 'mapping',
// 'iterable', 'sequence', 'callable', 'upper', 'lower', 'empty',
// 'in', 'eq', 'ne', 'sameas', 'equalto', 'truthy', 'falsy', ...]