Builtin Filter

lower

Convert letters to lowercase

upper

Convert letters to uppercase

wordcount

Outputs the number of words

capitalize

Capitalize sentence words

title

Capitalize each word

replace

Replaces the string

{{ name | replace(from="Robert", to="Bob")}}

addslashes

Quotation Conversion

{{ set value = "I'm using Daobox" }}
{{ value | addslashes }}

Output I\\\'m using Daobox.

slugify

Convert to a nameable string

{{ set value = "-Hello world! " }}
{{ value | slugify }}

Output hello-world

trim

Filtering spaces at both ends

trim_start

Filter left-hand spaces

trim_end

Filter right-hand spaces

trim_start_matches

Filter left-hand matches

{{ set value = "//a/b/c//" }}
{{ value | trim_start_matches(pat="//") }}

Outputting a/b/c//

trim_end_matches

Filter left-hand side matches

{{ set value = "//a/b/c//" }}
{{ value | trim_start_matches(pat="//") }}

Output //a/b/c.

truncate

Truncates a string of the specified length. If it is not long enough, keep it all. Add "…" to the end of the truncate. , the suffix can be changed with the end parameter.

{{ value | truncate(length=10, end="") }}

linebreaksbr

Replace line breaks \n, \r\n with <br>.

spaceless

Removes spaces and line breaks within HTML tags.

{% set value = "<p>\n<a> </a>\r\n </p>" %}
{{ value | spaceless }}

Output: <p><a></a></p>

striptags

Filtering HTML tags

{% set value = "<b>Joel</b>" %}
{{ value | striptags }}

Output: Joel

First

Returns the first element of the list, or the empty string if the list is empty.

last

Returns the last element of the list, or the empty string if the list is empty.

nth

Returns the Nth (numbered from 0) element of the list, or the empty string if the list is empty.

{{ value | nth(n=2) }}

join

Joins strings in a list using a separator character

{% set value = ['a', 'b', 'c'] %}
{{ value | join(sep=" // ") }}

Output: a // b // c

length

Returns the length of a list, an object, or a string.

index_of

Returns the position of the specified element in the list, starting at 0.

Parameters

Parameter Name Meaning Type Optional Description
search list of searches list of elements

Returns

integer. Returns -1 if not found.

e.g.

{% set s = "hello" %}
{{ s | index_of(search=["a", "b", "hello"]) }}

{# output: 2 #}

reverse

Returns a reverse-ordered string or list

sort

Sort the list. The elements of the list must be sortable.

  • Numbers by value size
  • Strings are sized by Asc
  • Lists by their length
  • Boolean, true=1, false=0

If the list elements are objects (key-value pairs), you can specify the attributes used for sorting

{% set people = [
  {
    "name": ["daobox", "yu"], "age": 30
    "age": 30
  }
] %}

{# Sort by first last name #}
{{ people | sort(attribute="name.0") }}

{# Sort by age #}
{{ people | sort(attribute="age") }}

unique

List de-duplication. Usage is the same as sort, when the reference value is a string, you can specify whether to ignore case by case_sensitive.

{{ people | unique(attribute="age") }}
{{ people | unique(attribute="name.1", case_sensitive="true") }}

slice

Slices the list. Specifies the start and end index numbers (starting at 0) with the arguments start, end, and returns a sublist from start (inclusive) to end (exclusive). Both arguments can be omitted, and default to both end positions.

{% for i in my_arr | slice(end=5) %}
{% for i in my_arr | slice(start=1) %}
{% for i in my_arr | slice(start=1, end=5) %}

Use an ordinal number less than 0 to indicate counting backwards from the end.

{% set my_array = [1, 2, 3, 4, 5] %}
{{ my_arr | slice(end=-2) }}

The result is: [1, 2, 3]

group_by

Groups a list. Specifies the attribute parameter as the basis for the grouping, and returns an object (key-value pair) with the attribute value as the key, and the corresponding value as a list of attribute-equivalents. Elements are ignored if they lack a grouping attribute.

The following object definition (pseudo-code)

Author {
    name: String,
};

Post {
    content: String,
    year: u32,
    author: Author,
}

posts=[Post, Post]

Grouping by year

{{ posts | group_by(attribute="year") }}

Grouping by authore.name

{% for name, author_posts in posts | group_by(attribute="author.name") %}
    {{ name }}
    {% for post in author_posts %}
        {{ post.year }}: {{ post.content }}
    {% endfor %}
{% endfor %}

Customizable grouping

{% set map = section.pages | group_by(attribute="years") %}
{% set_global years = [] %}
{% for year, ignored in map %}
    {% set_global years = years | concat(with=year) %}
{% endfor %}
{% for year in years | reverse %}
    {% set posts = map[year] %}
{% endfor %}
``

## filter

Filter the list. Only returns a list of elements with attributes equal to the specified value. Missing or empty attributes are ignored.

The `attribute` parameter is mandatory, and `value` can be omitted to indicate that elements with null attributes are filtered out.

``jinja2
Author {
    name: String,
};

Post {
    content: String,
    year: u32,
    author: Author,
    draft: bool,
}
{{ posts | filter(attribute="draft", value=true) }}
{{ posts | filter(attribute="author.name", value="Vincent") }}

map

Extracts the specified attributes of each element and returns a list.

Name(String, String).

Person {
    name: Name,
    age: u32, ``Jinja2
}

Returns all ages

{{ people | map(attribute="age") }}

concat

Append an element or group of elements to the list

{{ posts | concat(with=drafts) }}

urlencode

The transcoded string is a safe human URL encoding, and / is not involved in transcoding according to RFC3986.

{% set value = "/foo?a=b&c=d" %}
{{ value | urlencode }}

Output: /foo%3Fa%3Db%26c%3Dd

urlencode_strict

Functions as urlencode, but the transcoding includes /.

round

Intercepts floating point numbers.

Parameters:

  • method: Available values
    1. common:Rounding (default)
    2. ceil Upward Intercept
    3. floor Downward Intercept
  • precision: Defaults to 0

filesizeformat

Integers converted to friendly format.

{{ num | filesizeformat }}

date

Converts a timestamp or date to the specified date format.

{{ ts | date }}
{{ ts | date(format="%Y-%m-%d %H:%M") }}

The timezone parameter can be specified and defaults to Asia/Chongqing when omitted.

escape

Convert special characters

  • &&amp;
  • <&lt;
  • >&gt;
  • "&quot;
  • '&#x27;
  • /&#x2F;

escape_xml

Convert XML special characters

  • &&amp;
  • <&lt;
  • >&gt;
  • "&quot;
  • '&#x27;

safe

The usual content output will be transcoded with HTML special characters by default, safe controls the final content without HTML transcoding.

get

Retrieve the value in the object by key, support multi-level. The default parameter is optional, and is used to specify the default value to be returned if no key exists.

{{ sections | get(key="posts/content", default="default") }}

split

Splits a string into a list with the specified separator.

{{ path | split(pat="/") }}

int

Converts values to integers. Values are recognizable by the prefixes 0b, 0o, 0x.

Parameters:

  • default: optional. Default value to be used in case of conversion error.
  • base: optional. The number of bits in the integer. Valid values are 10, 2, 8, 16.

float

Convert to float. The default value to be used in case of conversion error can be specified with the default parameter.

json_encode

Convert to a standard json string. For security reasons, the channel box launcher will automatically convert HTML characters, so it can be used in conjunction with safe to disable transcoding.

{{ value | json_encode | safe }}

Parameters:

  • pretty: optional. Outputs a prettified JSON string. Valid values true or false.
{{ value | json_encode(pretty=true) | safe }}

default

Provides default values for undefined variables.

Note: Valid only for undefined variables. Not valid for empty strings (""), 0, false.

I would like to read more {{ "" | default (value="Louise Michel") }}!

Output: ``I would like to read more ! `

json

Convert text to JSON object.

{{ value | json }}

markdown_html

Alias md_html, converts Markdown text to HTML.

{{ value | md_html }}

thumbnail

Automatic thumbnail generation

Parameters

Key Meaning Type Optional Description
wxh <max_width>x<max_height> string ✔️ When omitted, the configuration item cfg.thumbnail.default_dimension is used for degradation, if not specified, the original image is used. Either height or width can be specified, eg. 400x (to limit the width to 400px) or x300 (to limit the height to 300px).

Returns

Thumbnail information.

Key Meaning Type Optional Description
url thumbnail address string
width Thumbnail width integer
height Thumbnail height integer

object_keys

Get a list of object keys

Returns

[
    "<key>",
]

object_values

Get a list of object values

Returns

[
    <value>,
]