length

Return the length of an array or string.

Input:

{% set arr = ["a", "b", "c"] %}
Number of elements in array is {{ arr | length }}

Output:

Number of elements in array is 3

sort

Same as http://mozilla.github.io/nunjucks/templating.html#sort-arr-reverse-casesens-attr

Although we support sort filter, it is recommended to use sort_array instead.

sort_array(path, order)

Sorts array of objects by values using their path, and sorts them using order, which is asc (default), or desc:

{{ arr | sort_array("home_sections.spotlight.weight", "asc") }}

Internally, this filter uses lodash orderBy function.

slice_array(start, end)

Creates a slice of array from start up to, but not including, end. Internally uses lodash slice method.

{% set arr = ["a", "b", "c", "d", "e"] %}
{% set sliced_arr = arr | slice_array(1, 4) %}

The sliced_arr will be:

["b", "c", "d"]

where(path, operator, match)

Filters array of objects by values using their path. This filter has three ways of invocation.

The following array will be used as an input value for the following examples.

arr = [
    {"a": {"b": null}},
    {"a": {"c": "foo"}},
    {"a": {"b": "bar", "c": "foo"}}
]

where(path)

If only path is specified, then array items are filtered by objects having a field in the specified path, regardless of their values:

{% set filtered_arr = arr | where("a.b") %}

The filtered_arr will have the following objects:

[{a: {b: null}}, {a: {b: "bar", c: "foo"}}]

where(path, match)

If first two parameters are specified, then == operator is assumed and array objects are filtered by values equal to match:

{% set filtered_arr = arr | where("a.c", "foo") %}

The filtered_arr will have the following objects:

[{a: {c: "foo"}}, {a: {b: "bar", c: "foo"}}]

where(path, operator, match)

Similar to where(path, match), but allows to specifying comparison operator which is one of the supported operators: ==, !=, >, >=, <, <=.

relative_url

If the filtered url starts with # or http, returns the url unchanged. Otherwise returns the url relative to baseurl. If config has urls_relative_to_base: true then URL will be relative to currently rendered page.

<link href={{ "assets/css/card.css" | relative_url }}>

link

Generates page url from page path. If the page path starts with #, returns the url unchanged.

Input:

{{ "posts/post1.md#hello" | link }}

If uglyUrls is set to true, then the output will be:

posts/post1.html#hello

Otherwise, the output will be:

posts/post1/index.html#hello

Note: if referenced page re-defines url in its frontmatter, then that url is returned instead.

safe

Mark the value as "safe" which means that this variable will not be HTML escaped.

For example, without the safe filter, the following input:

{{ "<p>hello world</p>" }}

will produce the following output:

&lt;p&gt;hello world&lt;/p&gt;

With safe filter applied, the following input:

{{ "<p>hello world</p>" | safe }}

will produce un-escaped HTML escaped

<p>hello world</p>

date_format(format[, type])

Formats a date object into representable string. type can be strftime or moment. The default is strftime which complies to strftime standard. If moment specified for type, then moment.js specific format is used.

Both following statements will format page.date and produce the same output, for example "Sunday, June 5, 2013":

{{ page.date | date_format("%A, %B %e, %Y") }}
{{ page.date | date_format("dddd, MMMM D, YYYY", "moment") }}

sprintf(format, arg)

Formats string using C like sprintf format specifier

Input:

{{ "Price: $%.2f" | format(2.1) }}

Output:

Price: $2.10

markdownify

Runs the filter input through markdown processor.

Input:

{% set markdown_text = "hello **world**" %}
{{ markdown_text | markdownify }}

Output:

<p>hello <b>world</b></p>

Note: due to markdown semantics, the output will always be wrapped with one or multiple paragraph elements (<p>). Therefore, if an HTML without block level tags needs to be generated, use regular HTML with safe filter.

nl2br

Converts new lines in string into <br/> elements. Useful when markdownify filter can not be used (see note for block-level tags inside markdown).

Input:

{% set plain_text = "hello\nworld" %}
{{ plain_text | nl2br | safe }}

Output:

hello<br/>world

split(separator)

Splits string by separator into array. Internally uses lodash split.

Input:

{% set words = "hello beautiful world" | split(" ") %}
<ul>
    {% for word in words %}<li>{{ word }}</li>{% endfor %}
</ul>

Output:

<ul>
    <li>hello</li>
    <li>beautiful</li>
    <li>world</li>
</ul>

append(str)

Appends str to the filtered string.

Input:

{{ "hello" | append(" beautiful") | append(" world") }}

Output:

hello beautiful world

starts_with

Checks if string starts with the given target string. Internally uses lodash startsWith.

Input:

{% if "hello" | starts_with("he") %}lorem ipsum{% endif %}

Output:

lorem ipsum

ends_with

Checks if string ends with the given target string. Internally uses lodash endsWith.

Input:

{% if "hello" | ends_with("lo") %}lorem ipsum{% endif %}

Output:

lorem ipsum

first

Get the first item in an array

Input:

{% set items = [1,2,3] %}
{{ items | first }}

Output:

1

last

Get the last item in an array

Input:

{% set items = [1,2,3] %}
{{ items | last }}

Output:

3

default(value, default, [boolean])

If value is strictly undefined, return default, otherwise value. If boolean is true(default: false), any JavaScript falsy value will return default (false, "", etc)

In order for Unibit site to be convertable by Stackbit to any other SSG, the boolean must be true.

Input:

{% set emptyString = "" %}
{% set someString = "foo" %}
{{ emptyString | default(someString, true) }}

Output:

someString