{{ variable }}
Jinja2 Template Essentials
A focused cheat sheet for Python backend developers, specifically designed for quick reference to Jinja2 templating syntax and features. Master variables, control flow, inheritance, and macros with practical examples.
Core Template Syntax
VARIABLES & EXPRESSIONS
|
Outputs the value of a variable. Jinja2 automatically escapes HTML by default (autoescape). |
|
Accesses attributes of an object.
|
|
Accesses elements of a list by index or dictionary by key.
|
|
Supports basic arithmetic operations. |
|
String concatenation using the |
|
Applies a filter to the variable’s value.
|
|
Filter with an argument (or no arguments).
|
Pro Tip |
Keep expressions simple. For complex logic, prepare data in your Python view function before passing to the template. |
CONTROL STRUCTURES
|
Basic conditional statement.
|
|
If-else block for alternative content.
|
|
Multiple conditional branches. |
|
Iterates over sequences.
|
|
An
|
|
Current iteration of the loop (1-based, 0-based).
|
|
Boolean, true if current item is the first/last in the iteration. |
Common Pitfall |
Using |
COMMON FILTERS (DATA MANIPULATION)
|
Provides a default value if the original value is undefined or false. |
|
Joins elements of a list with a specified separator.
|
|
Replaces all occurrences of a substring.
|
|
Removes leading and trailing whitespace. |
|
Returns the number of items in a sequence or characters in a string. |
|
Rounds a number to a specified precision. |
|
Capitalizes the first letter of the string. |
Pro Tip |
Chain filters: |
Template Organization & Reusability
INCLUDES & TEMPLATE INHERITANCE
|
Inserts the content of another template at the current location. |
|
Passes the current template’s context to the included template. This is often the default behavior. |
|
Prevents an error if the included template doesn’t exist. |
|
Inherits from a parent template, typically the first statement in a child template. |
|
Defines a block in the parent template that can be overridden by child templates. In
In
|
|
Renders the content of the parent block within an overridden block in a child template. |
Pro Tip |
Use |
MACROS & REUSABLE BLOCKS
|
Defines a macro (reusable function-like block of HTML) with arguments and default values.
|
|
Calls a macro by passing positional arguments. |
|
Calls a macro by passing keyword arguments. Useful for clarity with many arguments. |
|
Imports a specific macro from another template file. |
|
Imports all macros from a file into a namespace.
|
|
Allows passing a block of content to a macro using the Macro Definition:
|
Pro Tip |
Macros are excellent for creating reusable UI components (e.g., form fields, buttons, cards) that maintain consistent structure and styling across your application. |
COMMENTS & WHITESPACE
|
Comments are ignored by the parser and do not appear in the final output. |
|
For longer explanations or temporarily disabling blocks of code. |
|
Hyphens (
|
|
Hyphens after
|
|
While whitespace control in tags affects the template output, the |
Pro Tip |
Use whitespace control ( |
Advanced Filters & Tests
COMMON FILTERS (OUTPUT & LOGIC)
Note: Jinja2 auto-escapes HTML by default. |
|
Encodes a string for use in URLs (e.g., query parameters). |
|
Removes all HTML/XML tags from a string. |
|
Counts the number of words in a string. |
|
Sorts a dictionary by keys or values. Returns a list of (key, value) pairs.
|
|
Common Pitfall: Over-using |
|
TESTS
Checks if a variable is defined (exists in the context). |
|
Checks if a variable’s value is not |
|
Checks if a number is divisible by another number. |
|
Checks for equality. Can also use |
|
Checks if a sequence or mapping is empty. |
|
Checks if a value is a number (integer or float). |
|
Checks for membership (can also be used in |
|
Pro Tip: Tests are powerful for conditional rendering without writing complex expressions. Use them to check variable states, types, and properties cleanly. |
|