Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Add a default param for an archive page

Started by Samuele Masetto 9 years ago · 2 replies · 719 views
9 years ago

I have an archive page that shows current year events. In the same page users can click and view the previous events by a taxonomy list (2017, 2016, etc).

Now in my twig template I have this:

TWIG
    {% if uri.param('year') %}
      {% set year = uri.param('year') %}
      {% set collection = page.collection() %}
    {% else %}
      {% set year = "now"|date("Y") %}
      {% set collection = page.collection().dateRange('01/01/' ~ year, '12/31/' ~ year) %}
    {% endif %}

But when I call pagination number:

TWIG
    {% if config.plugins.pagination.enabled and page.collection.params.pagination %}
      {% include 'partials/pagination.html.twig' with {'pagination':page.collection.params.pagination} %}
    {% endif %}

page.collection.params.pagination does not contain year param.

I could add a default param in url like this:

TXT
mydomain/events/year:<current year>

How can I define a "default" param for a page?
Thanks

9 years ago

I was about to tell you all about Twig defaults fallbacks and testing for defined values, but I don't think that is your question!

So assuming your two code snippets are form the same file, it looks like you need to change the pagination parameter to use the collection variable you created above. You are using page.collection instead. Try:

TWIG
{% include 'partials/pagination.html.twig' with {'pagination': collection.params.pagination} %}
9 years ago

@hughbris:
Try:

{% include 'partials/pagination.html.twig' with {'pagination': collection.params.pagination} %}

The collection object has not "year" param set because it is not a parameter (I used only dateRange function). It doesn't work even if I use setParam collection function because pagination collection is set in onCollectionProcessedevent (see pagination.php in pagination plugin).

So I try adding in frontmatter:

YAML
routes:
    default: "/mypage/year:2018"

and it works, but year is not parametric.
If I use:

YAML
routes:
    default: "/eventi/year:{{ 'now'|date('Y') }}"
frontmatter:
    process_twig: true

it doesn't work.

Suggested topics

Topic Participants Replies Views Activity
Support · by webmasterodp, 7 days ago
4 152 6 days ago
Support · by nwhitfield, 1 week ago
2 132 1 week ago
Support · by stell, 3 weeks ago
5 368 2 weeks ago
Support · by gravinator, 1 month ago
5 557 2 weeks ago
Support · by Gravver, 3 weeks ago
6 344 3 weeks ago