Skip to content

Instantly share code, notes, and snippets.

@vipickering
Last active July 25, 2017 01:52
Show Gist options
  • Save vipickering/1901a0235d2e5f35126c5f13b43dd940 to your computer and use it in GitHub Desktop.
Save vipickering/1901a0235d2e5f35126c5f13b43dd940 to your computer and use it in GitHub Desktop.

Nunjucks Filters Missing Documentation

abs

Return the absolute value of the argument:

Input

{{ -3|abs }}

Output

3

batch

Return 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-56

capitalize

Make the first letter uppercase, the rest lower case:

Input

{{ "This Is A Test" | capitalize }}

Output

This is a test

center

Center the value in a field of a given width:

Input

{{ "fooo" | center }}

Output

fooo

dictsort

Sort 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 f

escape (aliased as e)

Convert 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

&lt;html&gt;

float

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

first

Get the first item in an array:

Input

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

Output

1

groupby

Group 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 jim

indent

Indent 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
    three

Change default indentation to 6 spaces:

Input

{{ "one\ntwo\nthree" | indent(6) }}

Output

one
      two
      three

Change default indentation to 6 spaces and indent the first line:

Input

{{ "one\ntwo\nthree" | indent(6, true) }}

Output

      one
      two
      three

int

Convert the value into an integer. If the conversion fails 0 is returned.

Input

{{ "3.5" | int }}

Output

3

join

Return a string which is the concatenation of the strings in a sequence:

Input

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

Output

123

The 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,bear

This behaviour is applicable to arrays:

Input

{% set items = [
    { name: 'foo' },
    { name: 'bar' },
    { name: 'bear' }]
%}

{{ items | join(",", "name") }}

Output

foo,bar,bear

last

Get the last item in an array:

Input

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

Output

3

length

Return the length of an array:

Input

{{ [1,2,3] | length }}

Output

3

list

Convert 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,

lower

Convert string to all lower case:

Input

{{ "fOObAr" | lower }}

Output

foobar

random

Select 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).

rejectattr (only the single-argument form)

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

1

replace

Replace 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.56

Insert 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

xxabbbccc

It is possible to search for patterns in a list to replace:

Input

{% set letters = "aaabbbccc" %}
{{ letters | replace("ab", "x", 2) }}

Output

aaxbbccc

reverse

Reverse a string:

Input

{{ "abcdef" | reverse }}

Output

fedcba

Reverse an array:

Input

{% for i in [1, 2, 3, 4] | reverse %}
    {{ i }}
{% endfor %}

Output

4 3 2 1

round

Round a number:

Input

{{ 4.5 | round }}

Output

5

Round to the nearest whole number (which rounds down):

Input

{{ 4 | round(0, "floor")

Output

4

Specify the number of digits to round:

Input

{{ 4.12346 | round(4) }}

Output

4.1235

safe

Mark 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> bar

selectattr (only the single-argument form)

Filter 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

2

slice

Slice 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>

string

Convert an object to a string:

Input

{% set item = 1234 %}
{% for i in item | string | list %}
    {{ i }},
{% endfor %}

Output

1,2,3,4,

sum

Output the sum of items in the array:

Input

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

Output

6

title

Make the first letter of the string uppercase:

Input

{{ "foo bar baz" | title }}

Output

Foo Bar Baz

trim

Strip leading and trailing whitespace:

Input

{{ "  foo " | trim }}

Output

foo

truncate

Return 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 ?

upper

Convert the string to upper case:

Input

{{ "foo" | upper }}

Output

FOO

urlencode

Escape strings for use in URLs, using UTF-8 encoding. Accepts both dictionaries and regular strings as well as pairwise iterables.

Input

{{ "&" | urlencode }}

Output

%26

urlize

Convert 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> bar

Truncate 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>

wordcount

Count and output the number of words in a string:

Input

{% set foo = "Hello World"%}
{{ foo | wordcount }}

Output

2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment