Skip to main content

Default Filters

Filters transform data before it's rendered. They're applied using the pipe (|) character after a variable. APITemplate.io supports all standard Jinja2 filters out of the box.

Syntax

{{ variable | filter_name }}

You can chain multiple filters:

{{ name | lower | capitalize }}

You can also pass arguments to filters:

{{ items | join(", ") }}
{{ [x, y, z] | join | upper }}

Text filters

capitalize

Capitalizes the first character of a string:

{{ "mango is fruit" | capitalize }}

Output: Mango is fruit

lower

Converts to lowercase:

{{ "Mango is a fruit" | lower }}

Output: mango is a fruit

upper

Converts to uppercase:

{{ "Mango is a fruit" | upper }}

Output: MANGO IS A FRUIT

title

Capitalizes the first letter of each word:

{{ "mango is fruit" | title }}

Output: Mango Is Fruit

replace

Replaces occurrences of a substring:

{{ "Hello World" | replace("World", "Jinja2") }}

Output: Hello Jinja2

default

Provides a fallback value when a variable is undefined or empty:

{{ company_name | default("Not specified") }}
{{ phone | default("N/A") }}

Number filters

max / min

Get the largest or smallest item from a list:

{{ [1, 2, 3] | max }}

Output: 3

round

Round a number:

{{ 3.14159 | round(2) }}

Output: 3.14

abs

Absolute value:

{{ -5 | abs }}

Output: 5

List filters

join

Join a list into a string:

{{ ["red", "green", "blue"] | join(", ") }}

Output: red, green, blue

length

Get the number of items:

{{ items | length }} items total

sum

Sum a list of numbers:

Total: {{ [10, 20, 30] | sum }}

Output: Total: 60

first / last

Get the first or last item:

{{ ["a", "b", "c"] | first }}

Output: a

map

Apply a filter or attribute lookup to a list:

{{ users | map(attribute='name') | join(", ") }}

random

Return a random item from a list:

{{ ["red", "green", "blue"] | random }}

Other built-in filters

Jinja2 includes many more filters. Here are some commonly used ones:

FilterDescriptionUsage
urlencodeURL-encode a stringurl | urlencode
formatApply printf-style formatting"%s - %s" | format("Hello", "World")
int / floatConvert to int or float"42" | int
trimStrip leading/trailing whitespacename | trim
truncateTruncate a stringtext | truncate(50)
wordcountCount words in a stringtext | wordcount
sortSort a listitems | sort
uniqueRemove duplicates from a listitems | unique
reverseReverse a list or stringitems | reverse
batchGroup items into batchesitems | batch(3)
selectattrFilter objects by attributeusers | selectattr("active")

For the complete list of Jinja2 built-in filters, see the official Jinja2 documentation.

Next steps