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.

Themes & Styling

User sorting date order on page collection

solved

Started by Matthew Appleton 8 years ago · 0 replies · 807 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 Pedro M, 2 months ago
4 195 2 months ago
Themes & Styling · by Ian, 2 months ago
3 91 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 451 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 45 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 125 3 months ago