Filters

Transform values in template expressions

4 min read 834 words

Filters transform values in template expressions using the pipe syntax.

Basic Usage

{{ name | upper }}
{{ title | truncate(50) }}
{{ items | join(", ") }}

Chaining Filters

{{ name | lower | capitalize }}
{{ text | striptags | truncate(100) }}

Pipeline Operator

Use|>for improved readability:

{{ title |> escape |> upper |> truncate(50) }}

Safe Pipeline (?|>)

None-propagating pipeline — if the value is None, all subsequent filters are skipped:

{{ user?.name ?|> upper ?|> trim ?? "Anonymous" }}

Optional Filter (?|)

Skip a single filter when the value is None:

{{ value ?| upper ?? "N/A" }}
{{ config?.debug ?| string ?? "unset" }}

Unlike | default("") | upper, ?| preserves falsy values (0, "", False) and only short-circuits onNone.

Common Filters

String Filters

Filter Description Example
upper Uppercase {{ "hello" \| upper }}HELLO
lower Lowercase {{ "HELLO" \| lower }}hello
capitalize Capitalize first {{ "hello" \| capitalize }}Hello
title Title case {{ "hello world" \| title }}Hello World
trim Strip whitespace {{ " hi " \| trim }}hi
truncate Shorten text {{ text \| truncate(50) }}
replace Replace text {{ s \| replace("a", "b") }}
striptags Remove HTML {{ html \| striptags }}

Collection Filters

Filter Description Example
first First item {{ items \| first }}
last Last item {{ items \| last }}
length Item count {{ items \| length }}
sort Sort items {{ items \| sort }}
reverse Reverse order {{ items \| reverse }}
unique Remove duplicates {{ items \| unique }}
join Concatenate {{ items \| join(", ") }}
take First N items {{ items \| take(5) }}
skip Skip N items {{ items \| skip(10) }}

CSS / HTML Filters

Filter Description Example
classes Join CSS classes, drop falsy {{ ["btn", "", "lg"] \| classes }}btn lg
escape HTML escape {{ html \| escape }}
safe Mark as safe {{ trusted \| safe }}
striptags Remove tags {{ html \| striptags }}

Number Filters

Filter Description Example
abs Absolute value {{ -5 \| abs }}5
decimal Fixed decimal places {{ 3.14159 \| decimal(2) }}3.14
round Round number {{ 3.7 \| round }}4
int Convert to int {{ "42" \| int }}42
float Convert to float {{ "3.14" \| float }}3.14

Utility Filters

Filter Description Example
default Fallback value {{ x \| default("N/A") }}
tojson JSON encode {{ data \| tojson }} · double-quoted attrs: {{ cfg \| tojson(attr=true) }}
pprint Pretty print {{ data \| pprint }}
debug Debug output {{ items \| debug }}

See Filter reference for alltojson parameters (indent, attr, etc.).

Filter Arguments

Filters can accept arguments:

{{ text | truncate(50) }}
{{ text | truncate(50, killwords=true) }}
{{ text | truncate(50, end="...") }}
{{ items | sort(attribute="date", reverse=true) }}

Sorting with Attributes

Sort objects by attribute:

{% for post in posts | sort(attribute="date") %}
    {{ post.title }}
{% end %}

{# Multiple attributes #}
{% for page in pages | sort(attribute="weight,title") %}
    {{ page.title }}
{% end %}

{# Reverse order #}
{% for post in posts | sort(attribute="date", reverse=true) %}
    {{ post.title }}
{% end %}

Grouping

Group items by attribute:

{% for group in posts | groupby("category") %}
    <h2>{{ group.grouper }}</h2>
    {% for post in group.list %}
        {{ post.title }}
    {% end %}
{% end %}

Filtering Items

{# Select items matching condition #}
{{ items | selectattr("is_active") }}
{{ items | selectattr("status", "eq", "published") }}

{# Reject items matching condition #}
{{ items | rejectattr("is_draft") }}

Safe HTML

Mark content as trusted HTML:

{{ html_content | safe }}

{# With reason for code review #}
{{ cms_block | safe(reason="sanitized by bleach library") }}

See Also