User sorting date order on page collection



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…

{% 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:

{% 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>