Skip to content

Instantly share code, notes, and snippets.

@wirwolf
Created January 24, 2026 21:28
Show Gist options
  • Select an option

  • Save wirwolf/033a9933d61252d4b065cbbec903e563 to your computer and use it in GitHub Desktop.

Select an option

Save wirwolf/033a9933d61252d4b065cbbec903e563 to your computer and use it in GitHub Desktop.
{# =====================================================================
COMMENTS (all forms)
===================================================================== #}
{# single line #}
{##
multiline
block
##}
{# =====================================================================
EXPRESSIONS & LITERALS
===================================================================== #}
{{ "string" }}
{{ 'string' }}
{{ 0 }}
{{ -1 }}
{{ 3.1415 }}
{{ true }}
{{ false }}
{{ none }}
{{ [] }}
{{ {} }}
{{ () }}
{{ [1, "a", true, none] }}
{{ {"a": 1, "b": [1, 2], "c": {"x": 1}} }}
{{ (1, 2, 3) }}
{{ "a" ~ 1 ~ true }}
{{ 1 + 2 }}
{{ 5 - 3 }}
{{ 2 * 3 }}
{{ 8 / 4 }}
{{ 7 // 3 }}
{{ 7 % 3 }}
{{ (1 + 2) * (3 + 4) }}
{{ 1 < 2 }}
{{ 2 > 1 }}
{{ 1 <= 1 }}
{{ 2 >= 1 }}
{{ 1 == 1 }}
{{ 1 != 2 }}
{{ true and false }}
{{ true or false }}
{{ not false }}
{{ a if a is defined else "fallback" }}
{# =====================================================================
SET STATEMENTS
===================================================================== #}
{% set a = 1 %}
{% set b = 2 %}
{% set c = a + b %}
{% set x, y = 10, 20 %}
{% set x = x + 1 %}
{% set ns = namespace(total=0, items=[]) %}
{% set ns.total = ns.total + 1 %}
{# =====================================================================
IF / ELIF / ELSE (deep nesting + expressions)
===================================================================== #}
{% if a is defined and a > 0 %}
{% if b is defined and b > 0 %}
BOTH POSITIVE
{% elif b == 0 %}
B ZERO
{% else %}
B NEGATIVE
{% endif %}
{% elif a == 0 %}
A ZERO
{% else %}
A NEGATIVE
{% endif %}
{# inline if-expression #}
{{ "yes" if a > 0 else "no" }}
{# =====================================================================
FOR LOOPS (all legal variants)
===================================================================== #}
{# simple for #}
{% for i in [1, 2, 3] %}
{{ i }}
{% endfor %}
{# for with unpacking #}
{% for k, v in {"a": 1, "b": 2}.items() %}
{{ k }}={{ v }}
{% endfor %}
{# for with if filter #}
{% for i in [1, 2, 3, 4, 5] if i is odd %}
ODD {{ i }}
{% endfor %}
{# for with loop variables #}
{% for i in [10, 20, 30] %}
{{ loop.index }} {{ loop.index0 }}
{{ loop.first }} {{ loop.last }}
{{ loop.length }}
{% endfor %}
{# nested for #}
{% for row in [[1, 2], [3, 4]] %}
{% for col in row %}
{{ col }}
{% endfor %}
{% endfor %}
{# for + else #}
{% for x in empty %}
{{ x }}
{% else %}
EMPTY ITERABLE
{% endfor %}
{# =====================================================================
FILTERS (all application forms)
===================================================================== #}
{{ "abc"|upper }}
{{ "ABC"|lower }}
{{ " abc "|trim }}
{{ [3, 2, 1]|sort }}
{{ {"a": 1, "b": 2}|length }}
{{ value|default("fallback") }}
{{ value|default("fallback", true) }}
{{ [1, 2, 3]|join(",") }}
{{ "hello"|replace("h", "H") }}
{{ " abc "|trim|upper|replace("A", "X") }}
{# filter in expression #}
{{ ("abc"|upper) == "ABC" }}
{# =====================================================================
TESTS (all base tests + compositions)
===================================================================== #}
{% if value is defined %}DEFINED{% endif %}
{% if value is not defined %}NOT DEFINED{% endif %}
{% if value is none %}NONE{% endif %}
{% if value is boolean %}BOOLEAN{% endif %}
{% if value is string %}STRING{% endif %}
{% if value is number %}NUMBER{% endif %}
{% if value is sequence %}SEQUENCE{% endif %}
{% if value is mapping %}MAPPING{% endif %}
{% if value is iterable %}ITERABLE{% endif %}
{% if value is callable %}CALLABLE{% endif %}
{% if 3 is odd %}ODD{% endif %}
{% if 4 is even %}EVEN{% endif %}
{% if 2 is greaterthan 1 %}GT{% endif %}
{% if 1 is lessthan 2 %}LT{% endif %}
{% if 2 is equalto 2 %}EQ{% endif %}
{% if "a" in ["a", "b", "c"] %}IN{% endif %}
{% if "x" not in ["a", "b"] %}NOT IN{% endif %}
{# =====================================================================
MACROS (args, defaults, nesting)
===================================================================== #}
{% macro greet(name, title="Mr.") %}
Hello {{ title }} {{ name }}
{% endmacro %}
{{ greet("John") }}
{{ greet("John", title="Dr.") }}
{% macro list_items(items) %}
{% for item in items %}
- {{ item }}
{% endfor %}
{% endmacro %}
{{ list_items([1, 2, 3]) }}
{% macro outer(value) %}
{% macro inner(v) %}
{{ v }}
{% endmacro %}
{{ inner(value) }}
{% endmacro %}
{{ outer("X") }}
{# =====================================================================
CALL / CALLER (nested)
===================================================================== #}
{% macro wrapper(tag="div") %}
<{{ tag }}>
{{ caller() }}
</{{ tag }}>
{% endmacro %}
{% call wrapper("section") %}
{% call wrapper("span") %}
TEXT
{% endcall %}
{% endcall %}
{# =====================================================================
BLOCKS (nested, override-safe)
===================================================================== #}
{% block header %}
HEADER
{% endblock %}
{% block content %}
{% block inner %}
INNER
{% endblock %}
{% endblock %}
{% block footer %}
FOOTER
{% endblock %}
{# =====================================================================
WHITESPACE CONTROL
===================================================================== #}
{%- if true -%}
TRIMMED
{%- endif -%}
{{- "LEFT" }}
{{ "RIGHT" -}}
{# =====================================================================
RAW
===================================================================== #}
{% raw %}
{{ not_evaluated }}
{% if also_not %}
{% endraw %}
{# =====================================================================
ESCAPING
===================================================================== #}
{{ "<b>bold</b>" }}
{{ "<b>bold</b>"|safe }}
{# =====================================================================
IMPORT / FROM (syntax only)
===================================================================== #}
{% import "macros.j2" as m %}
{% from "macros.j2" import greet as imported_greet %}
{# =====================================================================
INCLUDE (syntax only)
===================================================================== #}
{% include "partial.j2" %}
{# =====================================================================
EXTENDS (syntax only)
===================================================================== #}
{% extends "base.j2" %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment