Filter

Abs

Returns the absolute value of the argument.

Input:
(value = “number”, type = “number”, desc = “The number that you want to get the absolute value of”, required = true)

{% set my_number = -53 %}

{{ my_number|abs }}

Add

Adds a number to the existing value.

Input:
(value = “number”, type = “number”, desc = “Number or numeric variable to add to”, required = true)
Params:
(value = “addend”, type = “number”, desc = “The number added to the base number”, required = true)

{% set my_num = 40 %}

{{ my_num|add(13) }}

Attr

Renders the attribute of a dictionary.

Input:
(value = “obj”, desc = “The dictionary containing the attribute”, required = true)
Params:
(value = “name”, desc = “The dictionary attribute name to access”, required = true)

The filter example below is equivalent to rendering a variable that exists within a dictionary, such as content.absolute_url.

{{ content|attr('absolute_url') }}

Batch

A filter that groups up items within a sequence.

Input:
(value = “value”, desc = “The sequence or dict that the filter is applied to”, required = true)
Params:
(value = “linecount”, type = “number”, desc = “Number of items to include in the batch”, defaultValue = “0”)
_(value = “fillwith”, desc = “Value used to fill up missing items”)

{% set items=[1, 2, 3, 4, 5] %}
<table>
    {% for row in items|batch(3, 'xxx') %}
    <tr>
        {% for column in row %}
        <td>{{ column }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>

Output code:

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>4</td>
        <td>5</td>
        <td>xxx</td>
    </tr>
</table>

Bool

Converts a value into a boolean.

Input:
(value = “value”, desc = “The value to convert to a boolean”, required = true)

This example converts a text string value to a boolean.

{% if "true"|bool == true %}
    hello world
{% endif %}

Capitalize

Capitalizes a value. The first character will be uppercase, all others lowercase.

Input:
(value = “string”, desc = “String to capitalize the first letter of”, required = true)

{% set sentence = "the first letter of a sentence should always be capitalized." %}

{{ sentence|capitalize }}

Center

Uses whitespace to center the value in a field of a given width. This filter will only work in tags where whitespace is retained, such as <pre>.

Input:
(value = “value”, desc = “Value to center”, required = true)
Input:
(value = “width”, type = “number”, defaultValue = “80”, desc = “Width of field to center value in”)

<pre>
    {% set var = "string to center" %}

    {{ var|center(80) }}
</pre>

Count

Returns the number of items of a sequence or mapping.

Input:
(value = “object”, desc = “The sequence to count”, required = true)

Code:

{% set services = ['Web design', 'SEO', 'Inbound Marketing', 'PPC'] %}

{{ services|count }}

Cut

Removes a string from the value from another string.

Input:
(value = “value”, desc = “The original string”, required = true)
Params:
_(value = “toremove”, desc = “String to remove from the original string”, required = true)

{% set my_string = "Hello world." %}

{{ my_string|cut(' world') }}

DateTime or DatetimeFormat

Formats a date object.

Input:
(value = “value”, defaultValue = “current time”, desc = “The date variable or UNIX timestamp to format”, required = true)
Params:
(value = “format”, desc = “The format of the date determined by the directives added to this parameter”))
(value = “timezone”, defaultValue = “utc”, desc = “Time zone of output date”)

{% set time = my_variable|datetimeformat('%B %e, %Y') %}
{% set time = my_variable|datetimeformat('%a %A %w %d %e %b %B %m %y %Y %H %I %k %l %p %M %S %f %z %Z %j %U %W %c %x %X %%') %}

Default

If the value is undefined, it returns the passed default value, otherwise the value of the variable.

Input:
(value = “value”, desc = “The variable or value to test”, required = true)
Params:
_(value = “defaultvalue”, desc = “Value to print when variable is not defined”, required = true)
(value = “boolean”, type = “boolean”, defaultValue = “False”, desc = “Set to True to use with variables which evaluate to false”)

This will output the value of my_variable if the variable was defined, otherwise ‘my_variable is not defined.

{{ my_variable|default('my_variable is not defined') }}

If you want to use default with variables that evaluate to false you have to set the second parameter to true.

{{ ''|default('the string was empty', true) }}

DictSort

Sorts a dict and yields key-value pairs.

Input:
(value = “value”, desc = “Dict to sort”, required = true)
Params:
_(value = “casesensitive”, type = “boolean”, defaultValue = “False”, desc = “Determines whether or not the sorting is case sensitive”)
(value = “by”, type = “enum key|value”, defaultValue = “key”, desc = “Sort by dict key or value”)

Sort the dict by value, case insensitive.

{% for item in contact|dictsort(false, 'value') %}
    {{item}}
{% endfor %}

Divide

Divides the current value by a divisor.

Input:
(value = “value”, type = “number”, desc = “The numerator to be divided”, required = true)
Params:
(value = “divisor”, type = “number”, desc = “The divisor to divide the value”, required = true)

{% set numerator = 106 %}

{% numerator|divide(2) %}

Divisible

Evaluates to true if the value is divisible by the given number.

Input:
(value = “value”, type = “number”, desc = “The value to be divided”, required = true)
Params:
(value = “divisor”, type = “number”, desc = “The divisor to check if the value is divisible by”, required = true)

This example is an alternative to using the is divisibleby expression test.

{% set num = 10 %}

{% if num|divisible(2) %}
    The number is divisble by 2
{% endif %}

Escape

Converts the characters &, <, >, ‘,, and in a string to HTML-safe sequences. Use this filter if you need to display text that might contain such characters in HTML. Marks return value as markup string.

Input:
(value = “s”, desc = “String to escape”, required = true)

{% set escape_string = "<div>This markup is printed as text</div>" %}

{{ escape_string|escape }}

EscapeJinjava

Converts the characters { and } in strings to Jinjava-safe sequences. Use this filter if you need to display text that might contain such characters in Jinjava. Marks return value as markup string.

Input:
(value = “s”, desc = “String to escape”, required = true)

{% set escape_string = "{{This markup is printed as text}}" %}

{{ escape_string|escapeJinjava }}

EscapeJs

Escapes strings so that they can be safely inserted into a JavaScript variable declaration.

Input:
(value = “s”, desc = “String to escape”, required = true)

{% set escape_string = "This string can safely be inserted into JavaScript" %}

{{ escape_string|escapejs }}

EscapeJson

Escapes strings so that they can be used as JSON values.

Input:
(value = “s”, desc = “String to escape”, required = true)

{% set escape_string = "String that contains JavaScript" %}

{{ escape_string|escapejson }}

FileSizeFormat

Formats the value as a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc.).

Input:
(value = “value”, desc = “The value to convert to filesize format”, required = true)
Params:
(value = “binary”, type = “boolean”, defaultValue = “False”, desc = “Use binary prefixes (Mebi, Gibi)

{% set bytes = 100000 %}

{{ bytes|filesizeformat }}

First

Returns the first item of a sequence.

Input:
(value = “seq”, type = “sequence”, desc = “Sequence to return first item from”, required = true)

{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}

{{ my_sequence|first }}

Float

Converts the value into a floating point number.

Input:
(value = “value”, desc = “Value to convert to a float”, required = true)
Params:
(value = “default”, type = “float”, defaultValue = “0.0”, desc = “Value to return if conversion fails”)

This example converts a text field string value to a float.

{% set my_text = "25.3" %}

{{ my_text|float }}

ForceEscape

Enforces HTML escaping. This will probably double escape variables.

Input:
(value = “value”, desc = “Value to escape”, required = true)

{% set escape_string = "<div>This markup is printed as text</div>" %}

{{ escape_string|forceescape }}

Format

Applies Python string formatting to an object.

Input:
(value = “value”, desc = “String value to reformat”, required = true)
Params:
(value = “args”, type = “String…”, desc = “Values to insert into string”)

%s can be replaced with other variables or values.

{{ "Hi %s %s"|format("Hello", "World!") }}

GroupBy

Groups a sequence of objects by a common attribute.

Input:
(value = “value”, desc = “The dict to iterate through and group by a common attribute”, required = true)
Params:
(value = “attribute”, desc = “The common attribute to group by”, required = true)

If you have a list of dicts or objects that represent persons with gender, first_name and last_name attributes and you want to group all users by genders you can do something like this.

<ul>
    {% for group in contents|groupby('gender') %}
    <li>
        {{ group.grouper }}
        <ul>
        {% for content in group.list %}
            <li>
                {{ content.name }}
            </li>
        {% endfor %}
        </ul>
    </li>
    {% endfor %}
</ul>

Indent

Uses whitespace to indent a string.

Input:
(value = “string”, desc = “The string to indent”, required = true)
Params:
(value = “width”, type = “number”, defaultValue = “4”, desc = “Amount of whitespace to indent”)
(value = “indentfirst”, type = “boolean”, defaultValue = “False”, desc = “If True, first line will be indented”)

<pre>
    {% set var = "string to indent" %}

    {{ var|indent(2, true) }}
</pre>

Int

Converts the value into an integer.

Input:
(value = “value”, desc = “The value to convert to an integer”, required = true)
Params:
(value = “default”, type = “number”, defaultValue = “0”, desc = “Value to return if the conversion fails”)

This example converts a text field string value to a integer.

{% set my_text = "23" %}

{{ my_text|int }}

Join

Returns a string which is the concatenation of the strings in the sequence.

Input:
(value = “value”, desc = “The values to join”, required = true)
Params:
(value = “d”, desc = “The separator string used to join the items”, defaultValue = “(empty String)”)
(value = “attr”, desc = “Optional dict object attribute to use in joining”)

{{ [1, 2, 3]|join('|') }}

It is also possible to join certain attributes of an object

{{ users|join('|', attribute='username') }}

Last

Returns the last item of a sequence.

Input:
(value = “seq”, type = “sequence”, desc = “Sequence to return last item from”, required = true)

{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}

{{ my_sequence|last }}

Length

Returns the number of items in a sequence or mapping.

Input:
(value = “object”, desc = “The sequence to count”, required = true)

{% set services = ['Web design', 'SEO', 'Inbound Marketing', 'PPC'] %}

{{ services|length }}

List

Converts the value into a list. If it was a string, the returned list will be a list of characters.

Input:
(value = “value”, desc = “Value to add to a sequence”, required = true)

{% set one = 1 %}
{% set two = 2 %}
{% set three = 3 %}
{% set list_num = one|list + two|list + three|list %}

{{ list_num|list }}

Lower

Converts a value to lowercase.

Input:
(value = “s”, desc = “String to make lowercase”, required = true)

{{ "Text to MAKE Lowercase"|lowercase }}

Map

Applies a filter on a sequence of objects or looks up an attribute.

Input:
(value = “value”, type = “object”, desc = “Sequence to apply filter or dict to lookup attribute”, required = true)
Params:
(value = “attribute”, desc = “Filter to apply to an object or dict attribute to lookup”, required = true)

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.

Users on this page: {{ users|map(attribute='username')|join(', ') }}

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.

{% set seq = ['item1', 'item2', 'item3'] %}

{{ seq|map('upper') }}

Md5

Calculates the md5 hash of the given object.

Input:
(value = “value”, desc = “Value to get MD5 hash of”, required = true)

{{ content.absolute_url|md5 }}

Multiply

Multiplies the current object with the given multiplier.

Input:
(value = “value”, type = “number”, desc = “Base number to be multiplied”, required = true)
Params:
(value = “multiplier”, type = “number”, desc = “The multiplier”, required = true)

{% set n = 20 %}

{{ n|multiply(3) }}

PrettyPrint

Pretty print a variable. Useful for debugging.

Input:
(value = “value”, type = “object”, desc = “Object to Pretty Print”, required = true)

{% set this_var = "Variable that I want to debug" %}

{{ this_var|pprint }}

Random

Returns a random item from the sequence.

Input:
(value = “seq”, type = “sequence”, desc = “Sequence to return a random item from”, required = true)

The example below is a standard blog loop that returns a single random post.

{% for content in contents|random %}
    {{content}}
{% endfor %}

Reject

Filters a sequence of objects by applying a test to the object and rejecting the ones with the test succeeding.

Input:
(value = “seq”, type = “Sequence to test”, required = true)
Params:
_(value = “exptest”, type = “name of expression test”, desc = “Specify which expression test to run for making the selection”, required = true)

{% set some_numbers = [10, 12, 13, 3, 5, 17, 22] %}

{% some_numbers|reject('even') %}

RejectAttr

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.

Input:
(value = “seq”, type = “sequence”, desc = “Sequence to test”, required = true)
Params:
(value = “attribute”, desc = “Attribute to test for and reject items that contain it”, required = true)
_(value = “exptest”, type = “name of expression test”, defaultValue = “truthy”, desc = “Specify which expression test to run for making the rejection”)

This loop would reject any post containing content.post_list_summary_featured_image.

{% for content in contents|rejectattr('post_list_summary_featured_image') %}
    <div class="post-item">Post in listing markup</div>
{% endfor %}

Replace

Returns 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.

Input:
(value = “s”, desc = “Base string to find and replace within”, required = true)
Params:
(value = “old”, desc = “The old substring that you want to match and replace”, required = true)
(value = “new”, desc = “The new string that you replace the matched substring”, required = true)
(value = “count”, type = “number”, desc = “Replace only the first N occurrences”)

{{ "Hello World"|replace("Hello", "Goodbye") }}
{{ "aaaaargh"|replace("a", "d'oh, ", 2) }}

Reverse

Reverses the object or returns an iterator that iterates over it the other way round.

Input:
(value = “value”, type = “object”, desc = “The sequence or dict to reverse the iteration order”, required = true)

{% set nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] %}

{% for num in nums|reverse %}
    {{ num }}
{% endfor %}

Round

Rounds the number to a given precision.

Input:
(value = “value”, type = “number”, desc = “The number to round”, required = true)
Params:
(value = “precision”, type = “number”, defaultValue = “0”, desc = “Specifies the precision of rounding”)
(value = “method”, type = “enum common|ceil|floor”, defaultValue = “common”, desc = “Method of rounding: ‘common’ rounds either up or down, ‘ceil’ always rounds up, and ‘floor’ always rounds down.“)

Note that even if rounded to 0 precision, a float is returned.

{{ 42.55|round }}
{{ 42.55|round(1, 'floor') }}

If you need a real integer, pipe it through int.

{{ 42.55|round|int }}

Safe

Marks the value as safe, which means that in an environment with automatic escaping enabled, this variable will not be escaped.

Input:
(value = “value”, desc = “Value to mark as safe”, required = true)

{{ content.post_list_content|safe }}

Select

Filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.

Input:
(value = “sequence”, type = “sequence”, desc = “Sequence to test”, required = true)
Params:
_(value = “exptest”, type = “name of expression test”, defaultValue = “truthy”, desc = “Specify which expression test to run for making the selection”)

{% set some_numbers = [10, 12, 13, 3, 5, 17, 22] %}

{{ some_numbers|select('even') }}

SelectAttr

Filters a sequence of objects by applying a test to an attribute of an object and only selecting the ones with the test succeeding.

Input:
(value = “sequence”, type = “sequence”, desc = “Sequence to test”, required = true)
Params:
(value = “attr”, desc = “Attribute to test for and select items that contain it”, required = true)
_(value = “exptest”, type = “name of expression test”, defaultValue = “truthy”, desc = “Specify which expression test to run for making the selection”)

This loop would select any posts containing content.post_list_summary_featured_image.

{% for content in contents|selectattr('post_list_summary_featured_image') %}
    <div class="post-item">{{content}}</div>
{% endfor %}

Shuffle

Randomly shuffles a given list, returning a new list with all of the items of the original list in a random order.

Input:
(value = “sequence”, type = “sequence”, desc = “Sequence to shuffle”, required = true)

The example below is a standard blog loop, with order randomized on page load.

{% for content in ['a','b','c','d','e']|shuffle %}
    {{content}}
{% endfor %}

Slice

Slices an iterator and returns a list of lists containing those items.

Input:
(value = “value”, type = “sequence”, desc = “The sequence or dict that the filter is applied to”, required = true)
Params:
(value = “slices”, type = “number”, desc = “Specifies how many items will be sliced”, required = true)

Create a div containing three <ul> tags that represent columns.

{% set items = ['laptops', 'tablets', 'smartphones', 'smart watches', 'TVs'] %}

<div class="columwrapper">
    {% for column in items|slice(3) %}
    <ul class="column-{{ loop.index }}">
        {% for item in column %}
        <li>{{ item }}</li>
        {% endfor %}
    </ul>
    {% endfor %}
</div>

Sort

Sorts an iterable.

Input:
(value = “value”, type = “iterable”, desc = “The sequence or dict to sort through iteration”, required = true)
Params:
(value = “reverse”, type = “boolean”, defaultValue = “False”, desc = “Boolean to reverse the sort order”)
_(value = “casesensitive”, type = “boolean”, defaultValue = “False”, desc = “Determines whether or not the sorting is case sensitive”)
(value = “attribute”, desc = “Specifies an attribute to sort by”)

{% for item in [4,7,1,9,3,4,7,2,8,4,5,6,7,9]|sort %}
    {{item}}
{% endfor %}

Split

Splits the input string into a list on the given separator.

Input:
(value = “string”, desc = “The string to split”, required = true)
Params:
(value = “separator”, defaultValue = “ “, desc = “Specifies the separator to split the variable by”)
(value = “limit”, type = “number”, defaultValue = “0”, desc = “Limits resulting list by putting remainder of string into last list item”)

{% set string_to_split = "Stephen; David; Cait; Nancy; Mike; Joe; Niall; Tim; Amanda" %}

{% set names = string_to_split|split(';', 4) %}

<ul>
    {% for name in names %}
    <li>{{ name }}</li>
    {% endfor %}
</ul>

String

Returns string value of an object.

Input:
(value = “value”, desc = “The value to turn into a string”, required = true)

{% set number_to_string = 45 %}

{{ number_to_string|string }}

StripTags

Strip SGML/XML tags and replace adjacent whitespace by one space.

Input:
(value = “string”, desc = “string to strip tags from”, required = true)

{% set some_html = "<div><strong>Some text</strong> </div>" %}

{{ some_html|striptags }}

Sum

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.

Input:
(value = “value”, type = “iterable”, desc = “Selects the sequence or dict to sum values from”, required = true)
Params:
(value = “start”, type = “number”, defaultValue = “0”, desc = “Sets a value to return, if there is nothing in the variable to sum”)
(value = “attribute”, desc = “Specify an optional attribute of dict to sum”)

{% set sum_this = [1, 2, 3, 4, 5] %}

{{ sum_this|sum }}

Sum up only certain attributes.

Total: {{ items|sum(attribute='price') }}

Title

Returns a titlecased version of the value. Words will start with uppercase letters, all remaining characters are lowercase.

Input:
(value = “string”, type = “string”, desc = “the string to titlecase”, required = true)

{{ "My title should be titlecase"|title }}

Trim

Strips leading and trailing whitespace.

Input:
(value = “string”, type = “string”, desc = “the string to strip whitespace from”, required = true)

{{ " remove whitespace "|trim }}

Truncate

Returns 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 the specified length. Otherwise, it will discard the last word. If the text was in fact truncated, it will append an ellipsis (“…”). If you want to replace the ellipsis with another string, provide that string as the third parameter.

Input:
(value = “string”, desc = “The string to truncate”, required = true)
Params:
(value = “length”, type = “number”, defaultValue = “255”, desc = “Specifies the length at which to truncate the text (includes HTML characters)”)
(value = “killwords”, type = “boolean”, defaultValue = “False”, desc = “If true, the string will cut text at length”)
(value = “end”, defaultValue = “…”, desc = “The characters that will be added to indicate where the text was truncated”)

{{ "I only want to show the first sentence. Not the second."|truncate(40) }}
{{ "I only want to show the first sentence. Not the second."|truncate(35, True, '..') }}

TruncateHtml

Truncates a given string, respecting HTML markup (properly closes all nested tags).

Input:
(value = “html”, desc = “HTML to truncate”, required = true)
Params:
(value = “length”, type = “number”, defaultValue = “255”, desc = “Length at which to truncate text (HTML characters not included)”)
(value = “end”, defaultValue = “…”, desc = “The characters that will be added to indicate where the text was truncated”)
(value = “breakword”, type = “boolean”, defaultValue = “false”, desc = “If set to true, text will be truncated in the middle of words”)

{{ "<p>I want to truncate this text without breaking my HTML<p>"|truncatehtml(20, '..', false) }}

Unique

Extracts a unique set from a sequence of objects.

Input:
(value = “sequence”, type = “sequence”, desc = “Sequence to filter”, required = true)
Params:
(value = “attr”, type = “Optional attribute on object to use as unique identifier”)

Filter duplicated strings from a sequence of strings.

{{ ['foo', 'bar', 'foo', 'other']|unique|join(', ') }}

Filter out duplicate blog posts.

{% for content in contents|unique(attr='slug') %}
    {{ content }}
{% endfor %}

UnixTimestamp

Gets the UNIX timestamp value (in milliseconds) of a date object.

Input:
(value = “value”, defaultValue = “current time”, desc = “The date variable”, required = true)

{% mydatetime|unixtimestamp %}

Upper

Converts a value to uppercase.

Input:
(value = “string”, type = “string”, desc = “the string to uppercase”, required = true)

{{ "text to make uppercase"|uppercase }}

UrlEncode

Escapes strings for use in URLs (uses UTF-8 encoding). It accepts both dictionaries and regular strings, as well as pairwise iterables.

Input:
(value = “url”, type = “string”, desc = “the url to escape”, required = true)

{{ "Escape & URL encode this string"|urlencode }}

Urlize

Converts URLs in plain text into clickable links.

Input:
(value = “value”, type=“string”, desc = “string URL to convert to an anchor”, required = true)
Params:
_(value = “trim_urllimit”, type = “number”, desc = “Sets a character limit”)
(value = “nofollow”, type = “boolean”, defaultValue = “False”, desc = “Adds nofollow to generated link tag”)
(value = “target”, desc = “Adds target attr to generated link tag”)

Links are shortened to 40 chars and defined with rel=“nofollow”.

{{ "https://synerise.com"|urlize(40) }}

If target is specified, the target attribute will be added to the <a> tag

{{ "https://synerise.com"|urlize(10, true, target='_blank') }}

WordCount

Counts the words in the given string.

Input:
(value = “string”, type = “string”, desc = “string to count the words from”, required = true)

{%  set count_words = "Count the number of words in this variable" %}


{{ count_words|wordcount }}

WordWrap

Returns a copy of the string passed to the filter wrapped after 79 characters.

Input:
(value = “s”, type = “string”, desc = “String to wrap after a certain number of characters”, required = true)
Params:
(value = “width”, type = “number”, defaultValue = “79”, desc = “Sets the width of spaces at which to wrap the text”)
_(value = “break_longwords”, type = “boolean”, defaultValue = “True”, desc = “If true, long words will be broken when wrapped”)

<pre>
    {{ "Lorem ipsum dolor sit amet, consectetur adipiscing elit"|wordwrap(10) }}
</pre>

XmlAttr

Creates an HTML/XML attribute string based on the items in a dict.

Input:
(value = “dict”, type = “dict”, desc = “Dict to filter”, required = true)
Params:
(value = “autospace”, type = “boolean”, defaultValue = “True”, desc = “Automatically prepend a space in front of the item”)

{% set html_attributes = {'class': 'bold', 'id': 'sidebar'} %}

<div {{ html_attributes|xmlattr }}></div>
😕

We are sorry to hear that

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

😉

Awesome!

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.