Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Paginate a collection that was filtered by twig (CallbackFilterIterator)

Started by NicoHood 6 years ago · 1 replies · 1211 views
6 years ago

Hi,
I want to implement simple pagination BUT filter the list before via twig templates.

This is a normal pagination setup:

TWIG
{# Get the collection #}
{% set collection = page.collection() %}

{# Paginate collection #}
{% set limit = 1 %}
{% do paginate( collection, limit ) %}

{# Print paginated elements #}
{% for item in collection %}
  <h5>{{ item.title }}</h5>
{% endfor %}

{# Pagination navigation '<< 1 2 3 >>' #}
{% if config.plugins.pagination.enabled and collection.params.pagination %}
    {% include 'partials/pagination.html.twig' with {'base_url':page.url, 'pagination':collection.params.pagination} %}
{% endif %}

Now I want to add a special twig filter before:

TWIG
{# Filter collection (currently does not filter anything, as always returns true. Used to demonstrate the bug) #}
{% set collection = collection|filter(x => true) %}

I will then get an error:

Argument 1 passed to Grav\Plugin\PaginationHelper::__construct() must be an instance of Grav\Common\Page\Collection, instance of CallbackFilterIterator given

Now luckly I found a workaround, but I am not really happy with it:

TWIG
{# Filter collection (currently does not filter anything, as always returns true. Used to demonstrate the bug) #}
{% set collection_twigfilter = collection|filter(x => true) %}

{# Workaround to cast the filtered list back into a collection object to allow pagination #}
{% set collection = page.collection({'items': ''})%}
{% for collection_page in collection_twigfilter %}
    {% do collection.addPage(collection_page) %}
{% endfor %}

Is there a way to avoid that? I expected to have some collection|filter(x => true)|cast(collection) option?

6 years ago

Full workaround:

TWIG
{# Get the collection #}
{% set collection = page.collection() %}

{# Filter collection (currently does not filter anything, as always returns true. Used to demonstrate the bug) #}
{% set collection_twigfilter = collection|filter(x => true) %}

{# Workaround to cast the filtered list back into a collection object to allow pagination #}
{% set collection = page.collection({'items': ''})%}
{% for collection_page in collection_twigfilter %}
    {% do collection.addPage(collection_page) %}
{% endfor %}

{# Paginate collection #}
{% set limit = 1 %}
{% do paginate( collection, limit ) %}

{# Print paginated elements #}
{% for item in collection %}
  <h5>{{ item.title }}</h5>
{% endfor %}

{# Pagination navigation '<< 1 2 3 >>' #}
{% if config.plugins.pagination.enabled and collection.params.pagination %}
    {% include 'partials/pagination.html.twig' with {'base_url':page.url, 'pagination':collection.params.pagination} %}
{% endif %}

Suggested topics

Topic Participants Replies Views Activity
Support · by Anna, 4 days ago
1 96 4 days ago
Support · by Anna, 3 weeks ago
1 265 3 weeks ago
Support · by webmasterodp, 1 month ago
4 380 1 month ago
Support · by nwhitfield, 1 month ago
2 351 1 month ago
Support · by stell, 2 months ago
5 657 1 month ago