Reading — step 1 of 5
Read
~1 min readControl Flow
Conditionals
{% if condition %}
...
{% elif other %}
...
{% else %}
...
{% endif %}
Render based on condition truthiness.
Truthiness rules (per Python):
False,None,0,"",[],{}: falsy.- Everything else: truthy.
python
Expression evaluation:
- Operators:
==,!=,<,>,<=,>=,and,or,not,in. {{ x in items }}→ check membership.{{ name == "Alice" }}→ compare.
Operator precedence:
orlowest.andnext.nothigher.- Comparisons
==,<. - Arithmetic
+,*.
Implementation:
python
Short-circuit: and returns first falsy or last; or returns first truthy or last. Critical for null guards: {{ user and user.name }}.
Tests (Jinja2):
{% if x is none %}{% if items is iterable %}{% if n is divisibleby 3 %}
Custom tests register similar to filters.
Avoid complex logic in templates. If you need three nested ifs, refactor data preparation.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…