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:
- Create a file in the src/filters/ directory of your site or module.
- Prepend the filter filename with
filter_
. - 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:
-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 %}