Return the absolute value of the argument:
Input
{{ -3|abs }}Output
3Return a list of lists with the given number of items:
Input
{% set items = [1,2,3,4,5,6] %}
{% for item in items | batch(2) %}
-{% for items in item %}
{{ items }}
{% endfor %}
{% endfor %}Output
12-34-56Make the first letter uppercase, the rest lower case:
Input
{{ "This Is A Test" | capitalize }}Output
This is a testCenter the value in a field of a given width:
Input
{{ "fooo" | center }}Output
foooSort a dict and yield (key, value) pairs:
{% set items = {
'e': 1,
'd': 2,
'c': 3,
'a': 4,
'f': 5,
'b': 6
} %}
{% for item in items | dictsort %}
{{ item[0] }}
{% endfor %}Output
a b c d e fConvert the characters &, <, >, ‘, and ” in strings 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
Input
{{ "<html>" | escape }}Output
<html>Convert a value into a floating point number. If the conversion fails 0.0 is returned. This default can be overridden by using the first parameter.
Input
{{ "3.5" | float }}Output
3.5Get the first item in an array:
Input
{% set items = [1,2,3] %}
{{ items | first }}Output
1Group a sequence of objects by a common attribute:
Input
{% set items = [
{ name: 'james', type: 'green' },
{ name: 'john', type: 'blue' },
{ name: 'jim', type: 'blue' },
{ name: 'jessie', type: 'green' }
]
%}
{% for type, items in items | groupby("type") %}
<b>{{ type }}</b> :
{% for item in items %}
{{ item.name }}
{% endfor %}<br>
{% endfor %}Output
green : james jessie
blue : john jimIndent a string using spaces. Default behaviour is not to indent the first line. Default indentation is 4 spaces.
Input
{{ "one\ntwo\nthree" | indent }}Output
one
two
threeChange default indentation to 6 spaces:
Input
{{ "one\ntwo\nthree" | indent(6) }}Output
one
two
threeChange default indentation to 6 spaces and indent the first line:
Input
{{ "one\ntwo\nthree" | indent(6, true) }}Output
one
two
threeConvert the value into an integer. If the conversion fails 0 is returned.
Input
{{ "3.5" | int }}Output
3Return a string which is the concatenation of the strings in a sequence:
Input
{% set items = [1, 2, 3] %}
{{ items | join }}Output
123The separator between elements is an empty string by default which can be defined with an optional parameter:
Input
{% set items = ['foo', 'bar', 'bear'] %}
{{ items | join(",") }}Output
foo,bar,bearThis behaviour is applicable to arrays:
Input
{% set items = [
{ name: 'foo' },
{ name: 'bar' },
{ name: 'bear' }]
%}
{{ items | join(",", "name") }}Output
foo,bar,bearGet the last item in an array:
Input
{% set items = [1,2,3] %}
{{ items | last }}Output
3Return the length of an array:
Input
{{ [1,2,3] | length }}Output
3Convert the value into a list. If it was a string the returned list will be a list of characters.
Input
{% for i in "foobar" | list %}{{ i }},{% endfor %}Output
f,o,o,b,a,r,Convert string to all lower case:
Input
{{ "fOObAr" | lower }}Output
foobarSelect a random value from an array. (This will change everytime the page is refreshed).
Input
{{ [1,2,3,4,5,6,7,8,9] | random }}Output
A random value between 1-9 (inclusive).
Filter a sequence of objects by applying a test to the specified attribute of each object, and rejecting the objects with the test succeeding.
This is the opposite of selectattr filter.
If no test is specified, the attribute’s value will be evaluated as a boolean.
Input
{% set foods = [{tasty: true}, {tasty: false}, {tasty: true}]%}
{{ foods | rejectattr("tasty") | length }}Output
1Replace one item with another. The first item is the item to be replaced, the second item is the replaced value.
Input
{% set numbers = 123456 %}
{{ numbers | replace("4", ".") }}Output
123.56Insert a replaced item before and after a value, by adding quote marks and replacing them surrounding an item:
Input
{% set letters = aaabbbccc%}
{{ "letters" | replace("", ".") }}Output
.l.e.t.t.e.r.s.
Every instance of an item up to a given number (item to be replaced, item replacement, number to be replaced):
Input
{% set letters = "aaabbbccc" %}
{{ letters | replace("a", "x", 2) }}Note in this instance the required quote marks surrounding the list.
Output
xxabbbcccIt is possible to search for patterns in a list to replace:
Input
{% set letters = "aaabbbccc" %}
{{ letters | replace("ab", "x", 2) }}Output
aaxbbcccReverse a string:
Input
{{ "abcdef" | reverse }}Output
fedcbaReverse an array:
Input
{% for i in [1, 2, 3, 4] | reverse %}
{{ i }}
{% endfor %}Output
4 3 2 1Round a number:
Input
{{ 4.5 | round }}Output
5Round to the nearest whole number (which rounds down):
Input
{{ 4 | round(0, "floor")Output
4Specify the number of digits to round:
Input
{{ 4.12346 | round(4) }}Output
4.1235Mark the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped.
Input
{{ "foo http://www.example.com/ bar" | urlize | safe }}Output
foo <a href="http://www.example.com/">http://www.example.com/</a> barFilter a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding.
This is the opposite to rejectattr.
If no test is specified, the attribute’s value will be evaluated as a boolean.
Input
{% set foods = [{tasty: true}, {tasty: false}, {tasty: true}]%}
{{ foods | selectattr("tasty") | length }}Output
2Slice an iterator and return a list of lists containing those items:
Input
{% set arr = [1,2,3,4,5,6,7,8,9] %}
<div class="columwrapper">
{%- for items in arr | slice(3) %}
<ul class="column-{{ loop.index }}">
{%- for item in items %}
<li>{{ item }}</li>
{%- endfor %}
</ul>
{%- endfor %}
</div>Output
<div class="columwrapper">
<ul class="column-1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul class="column-2">
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<ul class="column-3">
<li>7</li>
<li>8</li>
<li>9</li>
</ul>
</div>Convert an object to a string:
Input
{% set item = 1234 %}
{% for i in item | string | list %}
{{ i }},
{% endfor %}Output
1,2,3,4,Output the sum of items in the array:
Input
{% set items = [1,2,3] %}
{{ items | sum }}Output
6Make the first letter of the string uppercase:
Input
{{ "foo bar baz" | title }}Output
Foo Bar BazStrip leading and trailing whitespace:
Input
{{ " foo " | trim }}Output
fooReturn 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 ("..."). A different ellipsis sign than "(...)" can be specified using the third parameter.
Truncate to 3 characters:
Input
{{ "foo bar" | truncate(3) }}Output
foo(...)Truncate to 6 characters and replace "..." with a "?":
Input
{{ "foo bar baz" | truncate(6, true, "?") }}Output
foo ba ?Convert the string to upper case:
Input
{{ "foo" | upper }}Output
FOOEscape strings for use in URLs, using UTF-8 encoding. Accepts both dictionaries and regular strings as well as pairwise iterables.
Input
{{ "&" | urlencode }}Output
%26Convert URLs in plain text into clickable links:
Input
{{ "foo http://www.example.com/ bar" | urlize | safe }}Output
foo <a href="http://www.example.com/">http://www.example.com/</a> barTruncate URL text by a given number:
Input
{{ "http://mozilla.github.io/" | urlize(10, true) | safe }}Output
<a href="http://mozilla.github.io/">http://moz</a>Count and output the number of words in a string:
Input
{% set foo = "Hello World"%}
{{ foo | wordcount }}
Output
2