Tests are boolean predicates used withisin conditionals.
Usage
{% if value is test %}
{% if value is test(arg) %}
{% if value is not test %}
Type Tests
defined
Value is not None.
{% if user is defined %}
{{ user.name }}
{% end %}
undefined
Value is None.
{% if user is undefined %}
<p>Not logged in</p>
{% end %}
none
Value is None (alias for undefined).
{% if value is none %}
string
Value is a string.
{% if value is string %}
{{ value | upper }}
{% end %}
number
Value is int or float (not bool).
{% if value is number %}
{{ value * 2 }}
{% end %}
sequence
Value is list, tuple, or string.
{% if items is sequence %}
{{ items | length }} items
{% end %}
mapping
Value is a dict.
{% if data is mapping %}
{{ data.keys() | list }}
{% end %}
iterable
Value supports iteration.
{% if items is iterable %}
{% for item in items %}...{% end %}
{% end %}
callable
Value is callable.
{% if func is callable %}
{{ func() }}
{% end %}
Boolean Tests
true
Value is exactly True.
{% if flag is true %}
Enabled
{% end %}
false
Value is exactly False.
{% if flag is false %}
Disabled
{% end %}
Number Tests
odd
Integer is odd.
{% if loop.index is odd %}
<tr class="odd">
{% end %}
even
Integer is even.
{% if loop.index is even %}
<tr class="even">
{% end %}
divisibleby
Integer is divisible by N.
{% if count is divisibleby(3) %}
Multiple of 3
{% end %}
Comparison Tests
eq / equalto
Equal to value.
{% if status is eq("active") %}
{% if count is equalto(0) %}
ne
Not equal to value.
{% if status is ne("deleted") %}
lt / lessthan
Less than value.
{% if count is lt(10) %}
{% if age is lessthan(18) %}
le
Less than or equal.
{% if score is le(100) %}
gt / greaterthan
Greater than value.
{% if count is gt(0) %}
{% if age is greaterthan(21) %}
ge
Greater than or equal.
{% if level is ge(5) %}
sameas
Identity comparison (is).
{% if a is sameas(b) %}
Same object
{% end %}
in
Value is in sequence.
{% if role is in(["admin", "moderator"]) %}
Has permissions
{% end %}
String Tests
lower
String is all lowercase.
{% if text is lower %}
Already lowercase
{% end %}
upper
String is all uppercase.
{% if text is upper %}
Already uppercase
{% end %}
match
String matches regex pattern.
{% if email is match(".*@.*\\..*") %}
Valid email format
{% end %}
Negation
Useis notto negate any test:
{% if value is not defined %}
{% if count is not even %}
{% if user is not none %}
{% if status is not eq("active") %}
Examples
Type Checking
{% if data is mapping %}
{% for key, value in data.items() %}
{{ key }}: {{ value }}
{% end %}
{% elif data is sequence %}
{% for item in data %}
{{ item }}
{% end %}
{% elif data is string %}
{{ data }}
{% end %}
Loop Styling
<table>
{% for row in data %}
<tr class="{% if loop.index is odd %}odd{% else %}even{% end %}">
<td>{{ row.name }}</td>
</tr>
{% end %}
</table>
Permission Checks
{% if user is defined %}
{% if user.role is in(["admin", "moderator"]) %}
{% include "admin-toolbar.html" %}
{% end %}
{% end %}
Pagination
{% if page is gt(1) %}
<a href="?page={{ page - 1 }}">Previous</a>
{% end %}
{% if page is lt(total_pages) %}
<a href="?page={{ page + 1 }}">Next</a>
{% end %}
See Also
- Control Flow — Conditionals and loops
- Custom Tests — Create tests