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 →

User sorting date order on page collection

solved

Started by Matthew Appleton 8 years ago · 0 replies · 1230 views
8 years ago

Just wanted to post this little trick which might be useful for people building a blog where they want to allow the user to sort post order.

— in this case reverse the date order... this is courtesy of @OleVik and his inspiration (Project Space skeleton) and @Ricardo (found on Discord) ...

First thing: set the page collection order for the 'parent' page in the page, in this instance @Ricardo suggested a way to extract the date parameter...

TWIG
{% set date_order = uri.params('date')|split(':')|last %}
{% set ordered_collection = page.collection.order('date', date_order|defined('desc')) %}

then, again thanks to @Ricardo we can toggle the date order like so:

TWIG
{% if date_order is sameas('asc') %}
                    {% set toggle = 'desc' %}
                    {% else %}
                    {% set toggle = 'asc ' %}
                    {% endif %}

                    <a class="btn" href="{{ base_url }}/my-page/date:{{toggle}}">REVERSE DATE ORDER</a>
👍 2

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by themetman, 6 days ago
1 103 2 days ago
Themes & Styling · by AH, 3 weeks ago
1 223 3 days ago
Themes & Styling · by Bruno, 4 years ago
6 1386 3 days ago
Themes & Styling · by TomW, 3 weeks ago
2 227 5 days ago
Themes & Styling · by FrViPofm, 2 months ago
4 687 2 months ago