Jinja filters

From Leo's Notes
Last edited on 15 February 2022, at 00:39.

Jinja filters allow you to run functions on some data right before it is rendered. It does not change the underlying data in anyway.

Quick introduction[edit | edit source]

  • Filters are applied using the pipe | operator.
  • You can chain multiple filters one after another to create a pipeline.
  • Some filters requires arguments. Pass them in as if calling a function.

Lists[edit | edit source]

All filters that create Lists or work on Lists create Generator objects instead of Lists. To convert these objects back to Lists, use "list" filter at the end

Cheatsheet[edit | edit source]

String filters[edit | edit source]

Function Example
upper, lower Converts string to upper or lower case apple | upper
replace(old, new, count=None) Replaces all or a specified number of occurrences of the old substring with the new substring apple | replace('a', 'b')
center(width=80) Centers the text based on the given width.

Note: there is no 'left' or 'right' filter. If you need to left or right align text, use format instead.

escape or e converted into html safe sequence by sanitising '&', '<', '>', ‘ (singlequote), and ” (doublequote).
format(args)
indent(width=4, first=False, blank=False) Indents text
capitalize The first character of the string is uppercased apple | capitalize
title Uppercases every word
trim Removes excess leading/trailing spaces
truncate(length=255, killwords=False, end='...', ) Truncates a string to the given length. Optionally specifies suffix if truncation happened.
wordwrap(width=79, break_long_words=True, wrapstring=None) Line wraps the given text into a specified column width.

Iterables[edit | edit source]

Iterables include lists, tuples, strings, and dictionaries.

Function Example
attr(name)
first / last / random Selects the first / last / or random item
batch Batches list items together items = [1, 2, 3, 4, 5]

items | batch(2, fill_with=8) | list

Output: [ [1, 2], [3, 4], [5, 8 ] ]

slice Slices a list into the given number of slices items = [1, 2, 3, 4, 5]

items | slice(2, fill_with=8) | list

Output: [ [1, 2, 3], [4, 5, 8 ] ]

map(attribute=attributeName) myDictList = [ {'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6} ]

myDictList | map(attribute='a') | list

Output: [1, 3, 5]

map(filter) Apply the given filter to every item in the list
reject(test) / select(test) Removes or retains items that fail the test
rejectattr(attribute, test, args)

selectattr(attribute, test, args)

Removes or retains items that have an attribute myDict = ({'a': 0}, {'a': 1}, {'a': 2}) %}

myDict | rejectattr('a', 'equalto', 1) | list

Output: [{'a': 0}, {'a': 2}]

unique returns all unique values
sort(reverse=False, case_sensitive=False, attribute=None) sort
reverse reverses order
shuffle randomly orders the list

Data types[edit | edit source]

Function Example
float(default) / int(default, base=10)
string Converts to Unicode string
list converts any iterable to a list
default(default='', bool=false) Provides a default value if the value is undefined. If given bool is true, None or empty string will be treated as undefined.
filesizeformat Converts to human readable sizes
from_json / json Converts from json to a dictionary object or an object into json.
length / count length of string or number of objects in a list

See also[edit | edit source]