Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Add a default param for an archive page

Started by Samuele Masetto 8 years ago · 2 replies · 505 views
8 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

8 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} %}
8 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 Thomas, 1 week ago
2 54 11 hours ago
Support · by Anna, 3 days ago
2 60 14 hours ago
Support · by Justin Young, 15 hours ago
1 30 15 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 57 5 days ago