.. _template_custom_filters:
.. highlight:: jinja
##############
Custom Filters
##############
Custom filters behave just like the default filters. You have to use the curly brackets {{ ... }} syntax. Check :ref:`template_filters` for more details about the usage.
Now it's getting intresting. Post Status Notifier provides custom filters specialized for the usage with WordPress.
.. _filter_divide:
******
divide
******
The filter ``divide`` divides an array into parts. The number of parts is defined by the argument.
This filter makes it easy to display array data in a column design. The example divides the post categories into three parts and uses two for loops (:ref:`for_loop`) to display the result in a three column design.
.. code::
{% for part in [post_categories_array]|divide(3) %}
{% for column in part %}
- {{ column }}
{% endfor %}
{% endfor %}
.. _filter_get_key:
*******
get_key
*******
You can extract a value from list data (e.g. created by the :ref:`filter_unserialize` filter) by using the custom filter ``get_key`` which takes a list key as parameter and returns its value. ``get_key`` works recursive through multidimensional lists. If the key does not exits an empty string will be returned.
.. code::
{{ edd_payment_meta|get_key("currency") }}
This will output "USD" from the example data above.
If you just want to use the list data once, you can combine both filters :ref:`filter_unserialize` and :ref:`filter_get_key` in one command:
.. code::
{{ [post_custom_field-_edd_payment_meta]|unserialize|get_key("currency") }}
.. _filter_unserialize:
***********
unserialize
***********
The **unserialize** filter comes in handy when you are dealing with list data provided by plugins. For example let's look at a part of the meta data **Crowdfunding by Astoundify** stores in the custom field "edd_payment_meta" which you can access with PSN's placeholder [post_custom_field-_edd_payment_meta]:
.. code-block:: php
a:6:{s:8:"currency";s:3:"USD";s:9:"downloads";s:73:"a:1:{i:0;a:3:{s:2:"id";s:2:"96";s:7:"options";a:0:{}s:8:"quantity";i:1;}} ... "
It's a serialized string of a PHP array which Crowdfunding by Astoundify uses internally to store and retrieve data related to a campaign. It would be great if we could use this information in a notifcation email.
So to use the serialized information you first have to **unserialize** the string and transform it into a list you can work with.
.. code::
{{ [post_custom_field-_edd_payment_meta]|unserialize }}
For reusability we can put the list data in a temporary variable, e.g. "edd_payment_meta", by using the **set** function.
.. code::
{% set edd_payment_meta = [post_custom_field-_edd_payment_meta]|unserialize %}