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

Add a default param for an archive page

Started by Samuele Masetto 8 years ago · 2 replies · 596 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 Anna, 19 hours ago
1 56 12 hours ago
Support · by gtx, 1 week ago
4 229 2 days ago
Support · by Paul Hodges, 2 weeks ago
13 277 5 days ago
Support · by Anna, 3 weeks ago
9 335 1 week ago
Support · by TomW, 2 weeks ago
4 189 2 weeks ago