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

Documentation of Find - Help Building a Recipe

Started by Muut Archive 11 years ago · 6 replies · 717 views
11 years ago

Good morning.

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.

Clues would be appreciated.

Thanks.

11 years ago

To clarify:

  1. traverse all children beneath a specified route (e.g., /).
  2. collect all children in an array/collection
  3. sort the array by date
  4. 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.

11 years ago

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.

Another option is to use page.collection which is populated by collection page headers.

A third approach would be to use a taxonomy.findTaxonomy call to retrieve pages, then manipulate the resulting collection to order by date.

Really there are many ways to achieve this.

11 years ago

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.

11 years ago

Hi

Within your MD file you usually have the page title as the first thing at the top so something like

TXT
title: Your Page Title

Within your twig file you can do {{ page.header.title }} and it outputs that onto the page...

11 years ago

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.

Thanks!

11 years ago

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.

TWIG

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1329 9 years ago
Archive · by Muut Archive, 9 years ago
2 922 9 years ago
Archive · by Muut Archive, 9 years ago
2 4051 9 years ago
Archive · by Muut Archive, 9 years ago
1 2930 9 years ago
Archive · by Muut Archive, 9 years ago
3 1108 9 years ago