Create a custom filter

Create custom template filters to change the way variables are rendered in your templates. By following some simple rules, Zotonic will automatically find the filter for you:

  1. Create a file in the src/filters/ directory of your site or module.
  2. Prepend the filter filename with filter_.
  3. Export a function with the name of your filter that corresponds to the filename.

So, let’s say you need to sort a list of items and remove duplicate values from it:

mod_yourmodule/filters/filter_uniquesort.erl
-module(filter_uniquesort).
-export([uniquesort/2]).
-include_lib("zotonic_core/include/zotonic.hrl").

uniquesort(List, _Context) ->
    lists:usort(List).

The custom uniquesort filter is then available in your templates:

{% for thing in list_of_things|uniquesort %}
    {{ thing }} is now sorted and unique!
{% endfor %}

See also