I am trying to build a twig template (a partial) that locates the last N updated pages under a particular route and lists them out: a “What’s New!” ability for a page. I assumed I could use the find() function to drive this iterative search.
However, I am having a little trouble finding the documentation I need. I am sure this is because I am coming to Grav and Twig very new and I am not sure where boundaries between things lie. I looked at the Twig doc site for information.
traverse all children beneath a specified route (e.g., /).
collect all children in an array/collection
sort the array by date
take the top nth children and make a “what’s new” list on the page
My hang-up is I don’t know the syntax well-enough and can’t seem to find a primer or coherent doc, so I am staring a twig templates trying to suss out the how.
What you might want to do is look at the related-pages plugin. This basically does some logic and uses dynamically assigned taxonomy to associate related pages. This is one approach.
Thank you. How, exactly, do you make a page.collection on page headers? There is no specific example of this on the link you pointed at.
In general, all of these seem to rely on inserted data into the pages specifically to be found later. I want to avoid some of this (for example, not all of these pages require, or even need, taxonomy). I suppose I could generally try to build a collection from page.header.title, but I am not exactly clear on the syntax to do so:
content:
items: page.header.title
Is this something, implied by some of the language on that page, that you must set up in the site.yaml file beforehand?
Again, specifics documentation examples, or code examples, would greatly be appreciated.
Thanks for the reply. Yes, I understand that. I was asking how I could use the collection mechanism to collect up all the files with a particular header.
Ah I see I have something similar on my site but not exactly what you want. Perhaps the below piece of code could be usefull for you? I basically needed to output all child folders onto a page and display an image, header text and link for it which the below does.
{% for p in page.find('/wallpapers').children if p != page %}
{{ dump(pages.find('/images/landing-page- thumbnails').media.images[p.header.artistimg~'.jpg']) }} {{p.header.artistimg}} <a href="{{p.url}}/">{{ p.title}}</a>
{% endfor %}
---