Custom Filters

ASA2 provides custom filters specialized for the use with WordPress. Custom filters behave exactly like the Default Filters. They must be appended to a placeholder with a pipe (|) like in this example:

{{ Title|custom_filter }}

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 product Features into three parts and uses two for loops (For loop) to display the result in a three column design.

<div>
    {% for part in FeaturesArray|divide(3) %}
        <div style="float: left; width: 33%;"><ul>
            {% for column in part %}
                <li>{{ column }}</li>
            {% endfor %}
        </ul></div>
    {% endfor %}
    <div style="clear: both;"></div>
</div>

get_key

You can extract a value from array data by using the custom filter get_key. It takes an array key as parameter and returns its value. get_key works recursive through multidimensional arrays. If the key does not exits an empty string will be returned.

This example will retrieve the second element of the product’s set of images:

{{ ImageSetsArray|get_key(2) }}

If you have serialized data, for example in a custom field, you can combine get_key with unserialize:

{{ repo_custom_field_serialized_array|unserialize|get_key("some_key") }}

unserialize

The filter unserialize comes in handy when you are dealing with serialized data, for example in Repo Custom Fields:

So to use the serialized data you first have to unserialize the string and transform it into a list (array) you can work with.

{{ repo_custom_field_serialized_array|unserialize }}

For reusability we can put the list data in a temporary variable, e.g. my_list_data, by using the set function.

{% set my_list_data = repo_custom_field_serialized_array|unserialize %}

Now you can use the varialbe my_list_data with array filters, like gallery:

{{ my_list_data|get_key("some_key") }}