Skip to content

rates_filter

The rates_filter macro is used to render a filter for specific product ratings.

Definition

{% rates_filter(filters, options) %}

Input parameters

filters

FilterValuesList an object of FilterValuesList representing values of a rates filter.

options

object represents an object of rates filter options and attributes

Option key Type Default Required Description
options.instanceId string "" yes Unique identifier commonly provided by special module variable moduleInstance
options.showCounters boolean false no If set to true the number of products meeting a criteria of a given filter will be displayed

Example

In this example we render a basic rates filter

{% from "@macros/rates_filter.twig" import rates_filter %}

{% set filters = ObjectApi.getFilters() %}

{{ rates_filter(filters.ratesFilter.values, {
    instanceId: moduleInstance
}) }}

Example

In this example we render a rates filter with counters

{% from "@macros/rates_filter.twig" import rates_filter %}

{% set filters = ObjectApi.getFilters() %}

{{ rates_filter(filters.ratesFilter.values, {
    instanceId: moduleInstance,
    showCounters: true
}) }}

Macro source code

{% macro rates_filter(filters, options) %}
    {% from "@macros/radio_control.twig" import radio_control %}
    {% from "@macros/star_score.twig" import star_score %}

    {% for filter in filters %}
        {% if loop.first %}
            {% set clearFilterId = filter.urlKey ~ options.instanceId %}

            <div class="filter-control">
                <div>
                    <div class="control">
                        <div class="radio">
                            {{ 
                                radio_control({
                                    id: clearFilterId,
                                    label: translate('Do not use this filter'),
                                    name: filter.urlKey,
                                    value: '',
                                    checked: not filters.isActive
                                }) 
                            }}

                            <div class="radio__content">
                                <label
                                    class="label"
                                    for={{ clearFilterId }}>{{ translate('Do not use this filter') }}</label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        {% endif %}

        {% set filterId = filter.urlKey ~ filter.urlValue ~ options.instanceId %}

        <div class="filter-control">
            <div>
                <div class="control">
                    <div class="radio">
                        {{ 
                            radio_control({
                                id: filterId,
                                label: filter.name,
                                name: filter.urlKey,
                                value: filter.urlValue,
                                checked: filter.isActive
                            }) 
                        }}

                        <div class="radio__content">
                            <label
                                class="label label_vcenter"
                                for={{ filterId }}>{{ filter.name }} {{ star_score(filter.urlValue, { numberOfStars: 5, size: 's' }) }}</label>
                        </div>
                    </div>
                </div>
            </div>

            {% if options.showCounters and filter.counter is defined %}
                <span class="filter-control__counter">{{ filter.counter }}</span>
            {% endif %}
        </div>
    {% endfor %}
{% endmacro %}