Flask Jinja2 Template Filters


What are template filters in Jinja2?

Template filters in Jinja2 allow you to modify the value of a variable before it is rendered in the template. Filters are applied using a pipe (|) symbol, and they can perform various operations like formatting strings, manipulating lists, or converting data types.

Example of applying a filter:

<p>{{ "hello world" | upper }}</p>

In this example, the upper filter converts the string "hello world" to uppercase, and the result is rendered as "HELLO WORLD".


How do you use the default filter in Jinja2?

The default filter provides a fallback value if a variable is undefined or evaluates to False. This ensures that your template renders properly, even if some data is missing.

Example of using the default filter:

{{ user | default('Guest') }}

In this example, if the user variable is undefined or evaluates to False, the string "Guest" will be displayed as a default value.


How do you use the length filter in Jinja2?

The length filter returns the number of items in a list, string, dictionary, or any other iterable. It is useful for checking the size of data structures in your templates.

Example of using the length filter:

<p>You have {{ items | length }} items in your list.</p>

In this example, the length filter returns the number of elements in the items list and displays it in the rendered HTML.


What is the safe filter in Jinja2?

The safe filter in Jinja2 marks a string as safe for rendering without escaping HTML characters. By default, Jinja2 escapes HTML to prevent cross-site scripting (XSS) attacks. The safe filter disables this behavior, allowing raw HTML to be rendered.

Example of using the safe filter:

<p>{{ "Bold Text" | safe }}</p>

In this example, the <strong> tag is rendered as raw HTML because the safe filter is applied. Without the safe filter, the tags would be escaped, and the result would be displayed as plain text.


How do you use the join filter in Jinja2?

The join filter in Jinja2 concatenates the elements of a list into a single string, using a specified separator. It is useful for displaying lists or arrays as formatted text in your templates.

Example of using the join filter:

<p>{{ items | join(', ') }}</p>

In this example, the items list is joined into a single string, with each element separated by a comma and a space. If items is ['apple', 'banana', 'cherry'], the result will be "apple, banana, cherry".


How do you use the capitalize filter in Jinja2?

The capitalize filter in Jinja2 converts the first character of a string to uppercase, while making all other characters lowercase.

Example of using the capitalize filter:

<p>{{ "hello world" | capitalize }}</p>

In this example, the capitalize filter transforms the string "hello world" into "Hello world".


How do you use the lower and upper filters in Jinja2?

The lower filter converts a string to lowercase, while the upper filter converts it to uppercase. These filters are useful for formatting text in your templates.

Example of using the lower and upper filters:

<p>{{ "HELLO" | lower }}</p>
<p>{{ "world" | upper }}</p>

In this example, lower transforms "HELLO" into "hello", and upper transforms "world" into "WORLD".


How do you use the round filter in Jinja2?

The round filter in Jinja2 rounds a number to a specified number of decimal places. If no precision is specified, it rounds to the nearest integer.

Example of using the round filter:

<p>{{ 3.14159 | round(2) }}</p>

In this example, the round filter rounds the number 3.14159 to two decimal places, resulting in 3.14.


How do you use the date filter in Jinja2?

The date filter in Jinja2 formats a date or datetime object according to a specified format string. This filter is useful for displaying dates in a human-readable format.

Example of using the date filter:

<p>{{ current_time | date('%Y-%m-%d') }}</p>

In this example, the date filter formats the current_time variable to display the date in the YYYY-MM-DD format.


What is the dictsort filter in Jinja2?

The dictsort filter in Jinja2 sorts a list of dictionaries by the specified key. This is useful for organizing data in tables or lists.

Example of using the dictsort filter:

<ul>
{% for user in users | dictsort('name') %}
    <li>{{ user.name }}</li>
{% endfor %}
</ul>

In this example, the dictsort filter sorts the users list of dictionaries by the name key before rendering the list.


How do you use the map filter in Jinja2?

The map filter in Jinja2 applies a specified attribute or function to each item in a list. This filter is useful when you need to extract or transform data from a list of objects or dictionaries.

Example of using the map filter:

<p>{{ users | map(attribute='username') | join(', ') }}</p>

In this example, the map filter extracts the username attribute from each user in the users list and joins them into a single string separated by commas.


How do you use the replace filter in Jinja2?

The replace filter in Jinja2 allows you to replace occurrences of a substring with a new value within a string. It is useful for modifying strings before rendering them in a template.

Example of using the replace filter:

<p>{{ "Hello, World!" | replace("World", "Jinja2") }}</p>

In this example, the replace filter replaces the word "World" with "Jinja2", resulting in the string "Hello, Jinja2!" being rendered.

Ads