Jinjava filters
Jinjava filters let you perform operations on values, such as rounding or formatting. If a filter has more than one parameter, the parameters are listed in the order they need to be provided.
You can find the snippets in our Github repository: https://github.com/Synerise/jinja-code-snippet
Abs
Returns the absolute value of the argument.
Input:
Type | Required | Description |
---|---|---|
number | yes | The number to calculate the absolute value from |
Example:
{% set my_number = -53 %}
{{ my_number|abs }} <!-- returns 53 -->
Add
Adds a number to the existing value.
Input:
Type | Required | Description |
---|---|---|
number | yes | The number to which the variable/number in the parameters will be added |
Parameters:
Type | Required | Description |
---|---|---|
number | yes | A number/variable |
Example:
{% set my_num = 40 %}
{{ my_num|add(13) }} <!-- returns 53 -->
Attr
Renders the attribute of a dictionary.
Input:
Type | Required | Description |
---|---|---|
object | yes | The dictionary that contains an attribute |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The name of the attribute to render |
Example:
The filter example below is equivalent to rendering a variable that exists within a dictionary, such as content.absolute_url.
{{ content|attr('absolute_url') }}
Base64 encode/decode
You can encode/decode values with base64.
{% set foo = "example@synerise.com"|base64Encode %}
{{ foo }}
<!-- returns ZXhhbXBsZUBzeW5lcmlzZS5jb20= -->
{% set baz = foo|base64Decode %}
{{ baz }}
<!-- returns "example@synerise.com -->
Batch
A filter that divides items in a list into groups.
Input:
Type | Required | Description |
---|---|---|
list | yes | A sequence or dict to apply the filter to |
Parameters:
Type | Required | Description |
---|---|---|
number | yes | The number of items to contain in each group |
string | no | The value used to fill in the positions of missing items |
Example:
{% 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:
Type | Required | Description |
---|---|---|
any | yes | The value to convert into a boolean |
Example:
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:
Type | Required | Description |
---|---|---|
string | yes | The string where the first character will be capitalized |
Example:
{% 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:
Type | Required | Description |
---|---|---|
value | yes | The value to center |
Parameters:
Type | Required | Description |
---|---|---|
number | yes | The width of the field where the value will be centered |
Example:
<pre>
{% set var = "string to center" %}
{{ var|center(80) }}
</pre>
Count
Returns the number of items in a sequence or mapping.
Input:
Type | Required | Description |
---|---|---|
value | yes | The sequence/mapping to count |
Example:
{% set services = ['Web design', 'SEO', 'Inbound Marketing', 'PPC'] %}
{{ services|count }}
Cut
Removes a string from the value of another string.
Input:
Type | Required | Description |
---|---|---|
string | yes | The original string |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The sub-string to remove from the original string |
Example:
{% set my_string = "Hello world." %}
{{ my_string|cut(' world') }}
DateTime or DatetimeFormat
Formats a date object.
Input:
Type | Required | Description |
---|---|---|
value | yes | The date variable to format, UNIX timestamp or ISO 8601 |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The format of the date determined by the directives added to this parameter |
string | no | Time zone of the output date |
Example:
{{ iso_date|iso8601_to_time|datetimeformat('%a, %B %d') }}
{{ timestamp|timestamp_to_time|datetimeformat('%a, %B %d') }}
Default
If the value is undefined, it returns the passed default value, otherwise the value of the variable.
Input:
Type | Required | Description |
---|---|---|
value | yes | The variable to check |
Parameters:
Type | Required | Description |
---|---|---|
value | yes | The value to return when the variable is not defined |
boolean | no | Set to true to use with variables which evaluate to false |
Example:
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 returns key-value pairs.
Input:
Type | Required | Description |
---|---|---|
dict | yes | The dict to sort |
Parameters:
Type | Required | Description |
---|---|---|
boolean | no | Defines if sorting is case-sensitive. Defaults to false . |
string | no | Allowed values: key ,value . Defines sorting by key or by value. Defaults to key . |
Example:
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:
Type | Required | Description |
---|---|---|
number | yes | The number to be divided |
Parameters:
Type | Required | Description |
---|---|---|
number | yes | The divisor |
Example:
{% set my_number = 106 %}
{{ my_number|divide(2) }}
Divisible
Evaluates to true
if the value is divisible by the divisor provided in the parameter.
Input:
Type | Required | Description |
---|---|---|
number | yes | The number to be divided |
Parameters:
Type | Required | Description |
---|---|---|
number | yes | The divisor |
Example:
This example is an alternative to using the is divisibleby expression test.
{% set num = 10 %}
{%- if num|divisible(2) -%}
The number is divisible 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 the return value as a markup string.
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to escape |
Example:
{% set escape_string = "<div>This markup is printed as text</div>" %}
{{ escape_string|escape }}
Escape_jinjava
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 the return value as a markup string.
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to escape |
Example:
{% set escape_string = "{{This markup is printed as text}}" %}
{{ escape_string|escape_jinjava }}
EscapeJs
Escapes strings so that they can be safely inserted into a JavaScript variable declaration.
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to escape |
Example:
{% 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:
Type | Required | Description |
---|---|---|
string | yes | The string to escape |
Example:
{% set escape_string = "String that contains JavaScript" %}
{{ escape_string|escapejson }}
FileSizeFormat
Formats raw file size in bytes into a human-readable format (for example, “13 kB”, “4.1 MB”, “102 bytes”, and so on).
Input:
Type | Required | Description |
---|---|---|
number | yes | The file size to format |
Parameters:
Type | Required | Description |
---|---|---|
boolean | no | Defines if binary prefixes (Mebi, Gibi) should be used. Defaults to false . |
Example:
{% set bytes = 100000 %}
{{ bytes|filesizeformat }}
First
Returns the first item of a sequence.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to process |
Example:
{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}
{{ my_sequence|first }}
Float
Converts the value into a floating point number.
Input:
Type | Required | Description |
---|---|---|
value | yes | The value to convert into a float |
Parameters:
Type | Required | Description |
---|---|---|
float | no | The value to return if conversion fails. Defaults to 0.0 . |
Example:
This example converts a text field string value to a float.
{% set my_text = "25.3" %}
{{ my_text|float }}
ForceEscape
Enforces HTML escaping.
Input:
Type | Required | Description |
---|---|---|
string | yes | The value to escape |
Example:
{% set escape_string = "<div>This markup is printed as text</div>" %}
{{ escape_string|forceescape }}
Format
Applies Python string formatting to an object.
Input:
Type | Required | Description |
---|---|---|
string | yes | String value to reformat |
Parameters:
Type | Required | Description |
---|---|---|
string arguments | yes | Values to insert into the string |
Example:
%s
can be replaced with other variables or values, for example %d
.
{{ "Hi %s %s"|format("Hello", "World!") }}
fromjson
Deserializes data from a string. The string must be a serialized object.
Input:
Type | Required | Description |
---|---|---|
string | yes | The JSON string to deserialize |
Example:
{% set y = '{"dataX":"b"}' %}
{% set deserialized = y|fromjson %}
Using fromjson with arrays
To use fromjson
with arrays, the array must be transformed into an object and then serialized with tojson.
Such a situation may occur when arrays are the result of macros or other inserts.
In the following example, the array is created for demonstration purposes:
// Set the array:
{% set array = [{"a":9,"b":10},{"a":12,"b":15}] %}
// Put the array in an object:
{% set obj = { data:array } %}
// Serialize the object:
{% set json_obj = obj|tojson %}
// Deserialize the object with the array:
{% set transformed = json_obj|fromjson %}
{{ transformed['data'][0].a }}
The output is 9
GroupBy
Groups a sequence of objects by a common attribute.
Input:
Type | Required | Description |
---|---|---|
dict | yes | The dict to iterate over and group by a common attribute |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The name of the common attribute to group by |
Example:
If you have a list of dicts or objects that represent persons with the attributes gender
, first_name
, and last_name
attributes, you can group all the customers by gender this way:
<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>
Hash
Returns the SHA-256 hash of a string.
{% set foo = "example@synerise.com"|hash("SHA-256") %}
{{ foo }}
<!-- returns cab06d7019d42ed33dcb260dba8860f8028d243dd78184f3b5156d7bdae636dd -->
Indent
Uses whitespace to indent a string.
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to indent |
Parameters:
Type | Required | Description |
---|---|---|
number | no | The number of spaces. Defaults to 4. |
boolean | no | If true , the first line will be indented. Defaults to false . |
Example:
<pre>
{% set var = "string to indent" %}
{{ var|indent(2, true) }}
</pre>
Int
Converts the value into an integer.
Input:
Type | Required | Description |
---|---|---|
value | yes | The value to convert into an integer |
Parameters:
Type | Required | Description |
---|---|---|
value | no | The value to return if conversion fails. Defaults to 0 . |
Example:
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 values in the sequence.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | A list of values to join |
Parameters:
Type | Required | Description |
---|---|---|
string | no | The separator. Defaults to empty string. |
string | no | Dict object attribute to use in joining |
Example:
{{ [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:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to process |
Example:
{% set my_sequence = ['Item 1', 'Item 2', 'Item 3'] %}
{{ my_sequence|last }}
Length
Returns the number of items in a sequence or mapping.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to process |
Example:
{% 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:
Type | Required | Description |
---|---|---|
value | yes | Value to split into a list |
Example:
{% 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:
Type | Required | Description |
---|---|---|
string | yes | The string to convert into lowercase |
Example:
{{ "Text to MAKE Lowercase"|lower }}
Map
Applies a filter on a sequence of objects or looks up an attribute.
Input:
Type | Required | Description |
---|---|---|
object | yes | Sequence to apply a filter to or a dict for attribute lookup |
Parameters:
Type | Required | Description |
---|---|---|
attribute pointer or string | yes | Filter to apply to the sequence or dict attribute to look up |
Example:
The basic usage is mapping by an attribute. Imagine you have a list of customers but you are only interested in a list of usernames.
Users on this page: {{ users|map(attribute='username')|join(', ') }}
Alternatively, you can let 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:
Type | Required | Description |
---|---|---|
value | yes | Value for MD5 hash calculation |
Example:
{{ content.absolute_url|md5 }}
Multiply
Multiplies the current object with the given multiplier.
Input:
Type | Required | Description |
---|---|---|
number | yes | The number to be multiplied |
Parameters:
Type | Required | Description |
---|---|---|
number | yes | The multiplier |
Example:
{% set n = 20 %}
{{ n|multiply(3) }}
PrettyPrint
Pretty print a variable. Useful for debugging.
Input:
Type | Required | Description |
---|---|---|
value | yes | Object to pretty print |
Example:
{% set this_var = "Variable that I want to debug" %}
{{ this_var|pprint }}
Random
Returns a random item from the sequence.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | Sequence to return a random entity from |
Example:
The following example shows how to return the name of a random item from a recommendation:
{% recommendations3 campaignId=CAMPAIGN_ID %}
{% set randomItem = recommended_products3|random %}
{{ randomItem.name }}
{% endrecommendations3 %}
Reject
Filters a sequence of objects by applying a test to the objects and excluding the ones that match the test.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to test |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The name of the test to apply |
Example:
{% 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 and rejecting the objects that match the test.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to test |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The name of the attribute to test |
string | no | The name of the test to apply. Defaults to 'truthy' . |
Example:
This loop rejects any post with the content.post_list_summary_featured_image
attribute.
{%- 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:
Type | Required | Description |
---|---|---|
string | yes | Base string |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | String to replace |
string | yes | The replacement value |
number | no | This many first occurrences are replaced |
Example:
{{ "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:
Type | Required | Description |
---|---|---|
value | yes | The sequence or dict to reverse |
Example:
{% 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:
Type | Required | Description |
---|---|---|
number | yes | The number to round |
Parameters:
Type | Required | Description |
---|---|---|
number | no | The precision of rounding (digits after decimal point). Defaults to 0. |
string | no | The method of rounding. Allowed values: ceil (up), floor (down), common (down if fraction < .5 ). Defaults to common . |
Example:
.0
{{ 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:
Type | Required | Description |
---|---|---|
value | yes | The value to mark as safe |
Example:
{{ content.post_list_content|safe }}
Select
Filters a sequence of objects by applying a test to the objects and only returning the ones that match the test.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to test |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The name of the test to apply |
Example:
{% 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 returning only the objects that match the test.
Input:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to test |
Parameters:
Type | Required | Description |
---|---|---|
string | yes | The name of the attribute to test |
string | no | The name of the test to apply. Defaults to 'truthy' . |
Example:
This loop selects 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:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to shuffle |
Example:
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:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence or dict to slice |
Parameters:
Type | Required | Description |
---|---|---|
number | yes | The number of slices to create |
Example:
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>
Output:
<div class="columwrapper">
<ul class="column-1">
<li>laptops</li>
<li>tablets</li>
</ul>
<ul class="column-2">
<li>smartphones</li>
<li>smart watches</li>
</ul>
<ul class="column-3">
<li>TVs</li>
</ul>
</div>
Sort
Sorts an iterable.
Input:
Type | Required | Description |
---|---|---|
iterable | yes | The sequence or dict to sort |
Parameters:
Type | Required | Description |
---|---|---|
boolean | no | If true , the sorting order is reversed. Defaults to false . |
boolean | no | If true , sorting is case-sensitive. Defaults to false . |
attribute | yes, if dict | If the input is a dict, this is the name of the attribute to sort by. |
Example:
{%- 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:
Type | Required | Description |
---|---|---|
string | yes | The string to split |
Parameters:
Type | Required | Description |
---|---|---|
string | no | The separator to split on. Defaults to a single space. (' ' ) |
number | no | The splitting stops after this many occurrences, the remaining items become the last item in the resulting list. |
Example:
{% 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 the string value of an object.
Input:
Type | Required | Description |
---|---|---|
value | yes | The value to return as a string |
Example:
{% set number_to_string = 45 %}
{{ number_to_string|string }}
StripTags
Strips SGML/XML tags and replaces adjacent whitespace by one space.
Input:
Type | Required | Description |
---|---|---|
string | yes | String to strip tags from |
Example:
{% 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 the start
parameter (which defaults to 0). When the sequence is empty, it returns start
.
Input:
Type | Required | Description |
---|---|---|
iterable | yes | A sequence or dict of values to sum |
Parameters:
Type | Required | Description |
---|---|---|
number | no | The start parameter. Defaults to 0 |
attribute | no | If the input is a dict, you can sum the values of an attribute. |
Example:
{% 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:
Type | Required | Description |
---|---|---|
string | yes | The string to transform |
Example:
{{ "My title should be titlecase"|title }}
tojson
Serializes data into a JSON string.
Input:
Type | Required | Description |
---|---|---|
various | yes | The data to transform into JSON |
Examples:
{% set x = {"dataX":"b"} %}
{% set x_to_json = x|tojson %}
{% set object = {
"field1": "value",
"field2": {
"subfield1": 1,
"subfield2": [
{
"nestedObjectField1": "value",
"nestedObjectField2": 1
},
{
"nestedObjectField1": "value",
"nestedObjectField2": 2
},
{
"nestedObjectField1": "value",
"nestedObjectField3": 3
}
]
}
} %}
{% set x = object|tojson %}
{% set qwe=[] %}
{% do qwe.append("123") %}
{% do qwe.append("456") %}
{% set transformed = qwe|tojson %}
Trim
Strips leading and trailing whitespace.
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to transform |
Example:
{{ " 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 exactly at the specified length. Otherwise, it will cut after the last complete word. If the text is actually truncated, the filter appends an ellipsis ("…"). If you want to replace the ellipsis with another string, provide that string as the third parameter.
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to transform |
Parameters:
Type | Required | Description |
---|---|---|
number | no | The number of characters to truncate after. Defaults to 255. |
boolean | no | If true , the string is truncated exactly after the specified number of characters. Otherwise, the text is truncated after the last complete word. Defaults to false . |
string | no | The string to append in the place where the text was truncated. Defaults to '...' |
Example:
{{ "I only want to show the first sentence. Not the second."|truncate(48, True) }}
<!-- I only want to show the first sentence. Not t... -->
{{ "I only want to show the first sentence. Not the second."|truncate(48, False) }}
<!-- I only want to show the first sentence. Not... -->
{{ "I only want to show the first sentence. Not the second."|truncate(35, True, '[...]') }}
<!-- I only want to show the first [...] -->
TruncateHtml
Truncates a given string, respecting HTML markup (properly closes all nested tags).
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to transform |
Parameters:
Type | Required | Description |
---|---|---|
number | no | The number of characters to truncate after. Defaults to 255. |
string | no | The string to append in the place where the text was truncated. Defaults to '...' |
boolean | no | If true , the string is truncated exactly after the specified number of characters. Otherwise, the text is truncated after the last complete word. Defaults to false . |
Example:
{{ "<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:
Type | Required | Description |
---|---|---|
sequence | yes | The sequence to filter |
Parameters:
Type | Required | Description |
---|---|---|
attr | no | If the input is a dict, you can use an attribute as the unique identifier. |
Example:
Filter duplicated strings from a sequence of strings.
{{ ['foo', 'bar', 'foo', 'other']|unique|join(', ') }}
<!-- foo, bar, other -->
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:
Type | Required | Description |
---|---|---|
value | yes | The date to convert |
Example:
{% mydatetime|unixtimestamp %}
Upper
Converts a value to uppercase.
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to convert into uppercase |
Example:
{{ "text to make uppercase"|upper }}
UrlEncode
Escapes strings for use in URLs (uses UTF-8 encoding). It accepts both dictionaries and regular strings, as well as pairwise iterables.
Input:
Type | Required | Description |
---|---|---|
string | yes | The URL to escape |
Example:
{{ "Escape & URL encode this string"|urlencode }}
Urlize
Converts URLs in plain text into clickable links.
Input:
Type | Required | Description |
---|---|---|
string | yes | String URL to convert into anchor |
Parameters:
Type | Required | Description |
---|---|---|
number | no | Sets a character limit |
boolean | no | If true , adds nofollow to the generated link. Defaults to false . |
target | no | Adds a target attribute to the generated <a> tag. |
Example:
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:
Type | Required | Description |
---|---|---|
string | yes | The string to process |
Example:
{% 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 a number of characters (79 by default).
Input:
Type | Required | Description |
---|---|---|
string | yes | The string to process |
Parameters:
Type | Required | Description |
---|---|---|
number | no | The number of characters to wrap after. Defaults to 79. |
boolean | no | If true , long words will be broken when wrapped. Defaults to true . |
Example:
<pre>
{{ "Lorem ipsum dolor sit amet, consectetur adipiscing elit"|wordwrap(10) }}
</pre>
Output:
Lorem
ipsum
dolor sit
amet, cons
ectetur
adipiscing
elit
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”)
Input:
Type | Required | Description |
---|---|---|
dict | yes | The dict to process |
Parameters:
Type | Required | Description |
---|---|---|
boolean | no | If true , automatically appends a space before the item. Defaults to true . |
Example:
{% set html_attributes = {'class': 'bold', 'id': 'sidebar'} %}
<div{{ html_attributes|xmlattr(False) }}></div>
<!-- <divclass="bold" id="sidebar"></div> -->
<div{{ html_attributes|xmlattr }}></div>
<!-- <div class="bold" id="sidebar"></div> -->