nodum.io Docs

Welcome at the nodum Docs. You'll find comprehensive guides and documentation to help you start working with nodumApp as quickly as possible. Let's jump right in!

Get Started    Docs

Filters 101

Modify values and variables using Twig Filters

Filters in nodum (using the Twig-language) allow you to alter the contents of a variable.

This could be a variable you set right in the template or one that is part of the data output provided by Craft (like when retrieving and looping through section entries).

Filters are applied right in the output tag, like this:
{{ variable | filter }}
and separated using the pipe character.

Here’s an example where we want to title case the string that is assigned to the variable.

{% set page_title = "an important news story" %}
{{ page_title | humanize }}

When outputting the contents of the page_title variable, we are running it through the humanize filter. This filter adjusts the string to title case, so every word starts with a capital letter.

It will output:

An Important News Story

Another very practical filter that you might use frequently is trim. This filter strips out whitespace at the beginning and end of a string.

{% set page_title = " an important news story " %}
{{ page_title | trim }}

That will remove the whitespace at the beginning and end of the string.

The previous example outputs:

an important news story

... which is missing the title casing we did before.

Let’s add in both filters together. We do that by chaining together them with pipes.

{{ page_title | trim | title }}
That outputs:

An Important News Story

Tip

When using filters it doesn't matter whether you use spaces between the variable and the filter(s).

Chaining filters

As is clearly shown in aforementioned example, filters can be set consecutively. This allows the output of every (preceding) filter to become the output of the successive filter.

When filters display array of object as output it is also possible to refer directly to keys after the filters:

{% set users = [ { name: "pepper" }, { name: "john" } ] %}
{{ users|first.name|humanize }}

{# Output: Pepper #}

As you can see in this example, it is possible to continue chaining with successive filters after referring to certain keys. In the example, the first value if the array users with the first filter is taken from the array, to take the key name thereof. Subsequently, the humanise filter makes every word start with a capital letter.

Filter documentation

Filters are categorised in different categories under the section "Filters" (see menu on the left).