jinja built-in
Return the absolute value of the argument.
{{ -1|abs }}|{{ 1|abs }}
# => 1
jinja built-in
Get an attribute of an object. foo|attr("bar") works like foo.bar just that always an attribute is returned and items are not looked up.
{{ foo|attr('bar') }}
# => 'quux'
jinja built-in
A filter that batches items. It works pretty much like slice just the other way round. It returns a list of lists with the given number of items. If you provide a second parameter this is used to fill up missing items.
{{ foo|batch(3)|list }}
# => [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
{{ foo|batch(3, 'X')|list }}
# => [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]
jinja built-in
Capitalize a string. The first character will be uppercase, all others lowercase.
{{ "foo bar"|capitalize }}
# => 'Foo bar'
jinja built-in
Centers the value in a field of a given width.
{{ "foo"|center(9) }}
# => ' foo '
jinja built-in
aliases: d
If the value is undefined it will return the passed default value, otherwise the value of the variable
{{ missing|default('no') }}
# => 'no'
{{ false|default('no') }}
# => 'false'
{{ false|default('no', true) }}
# => 'no'
{{ given|default('no') }}
# => 'yes'
jinja built-in
Sort a dict and yield (key, value) pairs. Because python dicts are unsorted you may want to use this function to order them by either key or value
{{ {"aa": 0, "b": 1, "c": 2, "AB": 3}|dictsort }}
# => [('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]
{{ {"aa": 0, "b": 1, "c": 2, "AB": 3}|dictsort(true) }}
# => [('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]
{{ {"aa": 0, "b": 1, "c": 2, "AB": 3}|dictsort(false, "value") }}
# => [('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]
jinja built-in
aliases: e
Convert the characters &, <, >, ‘, and ” in string s to HTML-safe sequences. Use this if you need to display text that might contain such characters in HTML. Marks return value as markup string.
{{ '<">&'|escape }}
# => '<">&'
jinja built-in
Format the value like a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega, Giga, etc.), if the second parameter is set to True the binary prefixes are used (Mebi, Gibi).
{{ 100|filesizeformat }}
# => 100 Bytes
{{ 1000|filesizeformat }}
# => 1.0 kB
{{ 1000000|filesizeformat }}
# => 1.0 MB
{{ 1000000000|filesizeformat }}
# => 1.0 GB
{{ 1000000000000|filesizeformat }}
# => 1.0 TB
{{ 100|filesizeformat(true) }}
# => 100 Bytes
{{ 1000|filesizeformat(true) }}
# => 1000 Bytes
{{ 1000000|filesizeformat(true) }}
# => 976.6 KiB
{{ 1000000000|filesizeformat(true) }}
# => 953.7 MiB
{{ 1000000000000|filesizeformat(true) }}
# => 931.3 GiB
jinja built-in
Return the first item of a sequence.
{{ list(range(10))|first }}
# => 0
jinja built-in
Convert the value into a floating point number. If the conversion doesn’t work it will return 0.0. You can override this default using the first parameter.
{{ "42"|float }}
# => 42.0
{{ "ajsghasjgd"|float }}
# => 0.0
{{ "32.32"|float }}
# => 32.32
jinja built-in
Enforce HTML escaping. This will probably double escape variables.
{{ '<div />'|forceescape }}
# => '<div />'
jinja built-in
Apply python string formatting on an object:
{{ "%s - %s"|format("Hello?", "Foo!") }}
# => Hello? - Foo!
jinja built-in
Group a sequence of objects by a common attribute.
[{'foo': 1, 'bar': 2},{'foo': 2, 'bar': 3},{'foo': 1, 'bar': 1},{'foo': 3, 'bar': 4}]|groupby('foo')
# => "1: 1, 2: 1, 1|2: 2, 3|3: 3, 4|"
[('a', 1), ('a', 2), ('b', 1)]|groupby(0)
# => 'a:1:2|b:1|'
jinja built-in
Return a copy of the passed string, each line indented by 4 spaces. The first line is not indented. If you want to change the number of spaces or indent the first line too you can pass additional parameters to the filter
{{ 'indent by two spaces and indent the first line too.'|indent(2, true) }}
# => ' indent by two spaces and indent the first line too.'
jinja built-in
Convert the value into an integer. If the conversion doesn’t work it will return 0. You can override this default using the first parameter. You can also override the default base (10) in the second parameter, which handles input with prefixes such as 0b, 0o and 0x for bases 2, 8 and 16 respectively.
{{ "42"|int }}
# => 42
{{ "ajsghasjgd"|int }}
# => 0
{{ "32.32"|int }}
# => 32
{{ "0x4d32"|int(0, 16) }}
# => 19762
{{ "011"|int(0, 8)}}
# => 9
{{ "0x33FU"|int(0, 16) }}
# => 0
jinja built-in
Return a string which is the concatenation of the strings in the sequence. The separator between elements is an empty string per default, you can define it with the optional parameter. It is also possible to join certain attributes of an object
{{ [1, 2, 3]|join('|') }}
# => 1|2|3
{{ [1, 2, 3]|join }}
# => 123
{{ map(User, ['foo', 'bar']))|join(', ', attribute='username') }}
# => 'foo, bar'
jinja built-in
Return the last item of a sequence.
{{ list(range(10))|last }}
# => 9
jinja built-in
aliases: count
Return the number of items of a sequence or mapping.
{{ "hello world"|length }}
# => 11
jinja built-in
Convert the value into a list. If it was a string the returned list will be a list of characters.
{{ "hello"|list }}
# => ['H', 'e', 'l', 'l', 'o']
jinja built-in
Convert a value to lowercase.
{{ "FOO"|lower }}
# => 'foo'
jinja built-in
Applies a filter on a sequence of objects or looks up an attribute. This is useful when dealing with lists of objects but you are really only interested in a certain value of it.
The basic usage is mapping on an attribute. Imagine you have a list of users but you are only interested in a list of usernames:
{{ [User('john'), User('jane'), User('mike')]|map(attribute="name")|join(",") }}
# => 'john,jane,mike'
Alternatively you can let it invoke a filter by passing the name of the filter and the arguments afterwards. A good example would be applying a text conversion filter on a sequence:
{{ ['John', 'Jane', 'Mike']| map('lower') | join(', ') }}
# => 'john, jane, mike'
New in jinja version 2.7.
jinja built-in
Pretty print a variable. Useful for debugging.
With Jinja 1.2 onwards you can pass it a parameter. If this parameter is truthy the output will be more verbose (this requires pretty)
{{[(0, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (1, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'}), (2, {'a': 'A', 'c': 'C', 'b': 'B', 'e': 'E', 'd': 'D', 'g': 'G', 'f': 'F', 'h': 'H'})] | pprint }}
# => [(0,
# {'a': 'A',
# 'b': 'B',
# 'c': 'C',
# 'd': 'D',
# 'e': 'E',
# 'f': 'F',
# 'g': 'G',
# 'h': 'H'}),
# (1,
# {'a': 'A',
# 'b': 'B',
# 'c': 'C',
# 'd': 'D',
# 'e': 'E',
# 'f': 'F',
# 'g': 'G',
# 'h': 'H'}),
# (2,
# {'a': 'A',
# 'b': 'B',
# 'c': 'C',
# 'd': 'D',
# 'e': 'E',
# 'f': 'F',
# 'g': 'G',
# 'h': 'H'})]
jinja built-in
Return a random item from the sequence.
{{ ['John', 'Jane', 'Mike'] | random }}
# => 'Jane'
{{ ['John', 'Jane', 'Mike'] | random }}
# => 'Mike'
jinja built-in
Filters a sequence of objects by applying a test to the object and rejecting the ones with the test succeeding.
{{ [1, 2, 3, 4, 5]|reject("odd")|join("|") }}
# => '2|4'
New in jinja version 2.7.
jinja built-in
Filters a sequence of objects by applying a test to an attribute of an object or the attribute and rejecting the ones with the test succeeding.
{{ [User('john', True), User('jane', True), User('mike', False)]|rejectattr("is_active")|map(attribute="name") }}
# => 'mike'
New in jinja version 2.7.
jinja built-in
Return a copy of the value with all occurrences of a substring replaced with a new one. The first argument is the substring that should be replaced, the second is the replacement string. If the optional third argument count is given, only the first count occurrences are replaced:
{{ "Hello World"|replace("Hello", "Goodbye") }}
# => Goodbye World
{{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
# => d'oh, d'oh, aaargh
jinja built-in
Reverse the object or return an iterator that iterates over it the other way round.
{{ "foobar"|reverse|join }} | {{ [1, 2, 3]|reverse|list }}
# => 'raboof|[3, 2, 1]'
jinja built-in
Round the number to a given precision. The first parameter specifies the precision (default is 0), the second the rounding method:
- 'common' rounds either up or down
- 'ceil' always rounds up
- 'floor' always rounds down
If you don’t specify a method 'common' is used.
{{ 42.55|round }}
# => 43.0
{{ 42.55|round(1, 'floor') }}
# => 42.5
Note that even if rounded to 0 precision, a float is returned. If you need a real integer, pipe it through int:
{{ 42.55|round|int }}
# => 43
jinja built-in
Mark the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped.
{{ "<div>foo</div>"|safe }}
# => '<div>foo</div>'
{{ "<div>foo</div>" }}
# => '<div>foo</div>'
jinja built-in
Filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.
{{ [1, 2, 3, 4, 5]|select("odd")|join("|") }}
# => '1|3|5'
New in jinja version 2.7.
jinja built-in
Filters a sequence of objects by applying a test to an attribute of an object and only selecting the ones with the test succeeding.
{{ [User('john', True), User('jane', True), User('mike', False)]|selectattr("is_active")|map(attribute="name") }}
# => 'john|jane'
New in jinja version 2.7.
jinja built-in
Slice an iterator and return a list of lists containing those items.
{{ list(range(10))|slice(3)|list }}
# => [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
{{ list(range(10))|slice(3, "X")|list }}
# => [[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]
If you pass it a second argument it’s used to fill missing values on the last iteration.
jinja built-in
Sort an iterable. Per default it sorts ascending, if you pass it true as first argument it will reverse the sorting.
If the iterable is made of strings the third parameter can be used to control the case sensitiveness of the comparison which is disabled by default.
{{ [2, 3, 1]|sort }}
# => [1, 2, 3]
{{ [2, 3, 1]|sort(true) }}
# => [3, 2, 1]
{{ "".join(["c", "A", "b", "D"]|sort) }}
# => 'AbcD'
{{ ['foo', 'Bar', 'blah']|sort }}
# => "['Bar', 'blah', 'foo']"
Changed in jinja version 2.6: The attribute parameter was added.
jinja built-in
Make a string unicode if it isn’t already. That way a markup string is not converted back to unicode.
{{ [1, 2, 3, 4, 5]|string }}
# => '12345'
jinja built-in
Strip SGML/XML tags and replace adjacent whitespace by one space.
{{ '<p>just a small \n <a href="#">example</a> link</p>\n<p>to a webpage</p><!-- <p>and some commented stuff</p> -->|striptags }}
# => 'just a small example link to a webpage'
jinja built-in
Returns the sum of a sequence of numbers plus the value of parameter ‘start’ (which defaults to 0). When the sequence is empty it returns start.
It is also possible to sum up only certain attributes:
{{ [1, 2, 3, 4, 5, 6]|sum }}
# => '21'
Changed in version 2.6: The attribute parameter was added to allow suming up over attributes. Also the start parameter was moved on to the right.
jinja built-in
Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.
{{ "foo bar"|title }}
# => "Foo Bar"
{{ "foo's bar"|title }}
# => Foo's Bar"
{{ "foo bar"|title }}
# => "Foo Bar"
{{ "f bar f"|title }}
# => "F Bar F"
{{ "foo-bar"|title }}
# => "Foo-Bar"
{{ "foo\tbar"|title }}
# => "Foo\tBar"
{{ "FOO\tBAR"|title }}
# => "Foo\tBar"
jinja built-in
Strip leading and trailing whitespace.
{{ " foo bar"|trim }}
# => "foo bar"
{{ " foo bar "|trim }}
# => "foo bar"
{{ "foo bar "|trim }}
# => "foo bar"
jinja built-in
Return a truncated copy of the string. The length is specified with the first parameter which defaults to 255. If the second parameter is true the filter will cut the text at length. Otherwise it will discard the last word. If the text was in fact truncated it will append an ellipsis sign ("..."). If you want a different ellipsis sign than "..." you can specify it using the third parameter.
{{ "foo bar baz"|truncate(9) }}
# => "foo ..."
{{ "foo bar baz"|truncate(9, True) }}
# => "foo ba..."
jinja built-in
Convert a value to uppercase.
{{ "foo"|upper }}
# => "FOO"
jinja built-in
Escape strings for use in URLs (uses UTF-8 encoding). It accepts both dictionaries and regular strings as well as pairwise iterables.
{{ "Hello, world!"|urlencode }}
# => 'Hello%2C%20world%21'
{{ "Hello, world\u203d"|urlencode }}
# => "Hello%2C%20world%E2%80%BD"
{{ ("f", 1),)"|urlencode }}
# => "f=1"
{{ (('f', 1), ("z", 2)) |urlencode }}
# => "f=1&z=2"
New in version 2.7.
jinja built-in
Converts URLs in plain text into clickable links.
If you pass the filter an additional integer it will shorten the urls to that number. Also a third argument exists that makes the urls “nofollow”:
{{ "foo http://www.example.com/ bar"|urlize }}
# => 'foo <a href="http://www.example.com/">http://www.example.com/</a> bar'
'{{ "foo http://www.example.com/ bar"|urlize(target="_blank") }}'
# => 'foo <a href="http://www.example.com/" target="_blank">http://www.example.com/</a> bar'
Changed in version 2.8+: The target parameter was added.
jinja built-in
Count the words in that string.
{{ "foo bar baz"|wordcount }}
# => 3
jinja built-in
Return a copy of the string passed to the filter wrapped after 79 characters. You can override this default using the first parameter. If you set the second parameter to false Jinja will not split words apart if they are longer than width. By default, the newlines will be the default newlines for the environment, but this can be changed using the wrapstring keyword argument.
# todo: supply example?
New in version 2.7: Added support for the wrapstring parameter.
jinja built-in
Create an SGML/XML attribute string based on the items in a dict. All values that are neither none nor undefined are automatically escaped:
{{ {'class': 'my_list', 'missing': none, 'id': 'list-%d'|format('42')}|xmlattr }}
# => class="my_list" id="list-42"