Jinja2 Filters
Filters let you transform values at render time.
Syntax:
{{ value | filter_name }} flowchart TD
A["user-supplied string with tags"] --> B{"how is it rendered?"}
B -->|"{{ x }}"| C["escaped: <script> …"]
C --> D["the browser shows it as text -- safe"]
B -->|"{{ x|safe }}"| E["inserted raw"]
B -->|"Markup(x) in Python"| E
E --> F["the browser EXECUTES it -- XSS"]
D --> G["use this for anything a user can influence"]
F --> H["use this only for markup you generated yourself"]
Common built-in filters
Section titled “Common built-in filters”<p>{{ username | lower }}</p>
<p>{{ username | upper }}</p>
<p>{{ items | join(", ") }}</p>Default values
Section titled “Default values”<p>{{ bio | default("No bio yet") }}</p>length
Section titled “length”<p>Count: {{ items | length }}</p>Important security note: safe
Section titled “Important security note: safe”|safe tells Jinja “don’t escape this value”.
Only use it if you’re absolutely sure the HTML is trusted.
Never mark raw user input as safe.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading