Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (28 sloc) 1.11 KB

Create a custom filter

Create custom :ref:`template filters <guide-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 :ref:`module-directory-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:

The custom uniquesort filter is then available in your templates:

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

    * :ref:`guide-templates` guide
    * :ref:`filters` reference