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

Sorting loops

Started by Muut Archive 10 years ago · 4 replies · 411 views
10 years ago

Can someone show me an example of how the twig sort filter works? I can't find good examples anywhere. I have this:

TWIG
{% set page_newsletter = page.find('/parents/newsletter') %}
{% for p in page_newsletter.children %}
  foo
{% endfor %}

I only want to get the first child page under /parents/newsletter ordered by date and sorted either asc or desc (so the oldest newsletter, or the latest).

Thanks

10 years ago

Hi Perlkonig, sorry - I did read that doc page but it doesn't tell me anything at all about how to use the actual function... and I still can't see, even from your example. Do you know how I could use the sort filter to sort a loop by date? I don't get it...

10 years ago

It completely depends on how your data and templates are structured. The typical way is through page collections, which is how all the blog templates work.

10 years ago

Hi Perlkonig, yeah I ended up working out how to make a page collection work for this. Thanks

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1354 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4064 9 years ago
Archive · by Muut Archive, 9 years ago
1 2951 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago