Skip to content

Commit

Permalink
doc: Add filter cookbook (fix #966)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Oct 7, 2016
1 parent 25de91b commit 92f7f60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 35 additions & 1 deletion doc/cookbook/custom-filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,38 @@
Create a custom filter
======================

.. todo:: this chapter
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:

.. code-block:: erlang
:caption: mod_yourmodule/filters/filter_uniquesort.erl
-module(filter_uniquesort).
-export([uniquesort/2]).
-include("zotonic.hrl").
uniquesort(List, _Context) ->
lists:usort(List).
The custom ``uniquesort`` filter is then available in your templates:

.. code-block:: django
{% for thing in list_of_things|unique %}
{{ thing }} is now sorted and unique!
{% endfor %}
.. seealso::

* :ref:`guide-templates` guide
* :ref:`filters` reference
2 changes: 2 additions & 0 deletions doc/developer-guide/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ The following naming conventions for templates are used:

.. seealso:: :ref:`guide-templates`

.. _module-directory-filters:

filters/
........

Expand Down

0 comments on commit 92f7f60

Please sign in to comment.