Dropmenu - how to limit number of pages?

@thgr, Let’s approach this the Grav way…

Have a look at the docs and especially the ones about Page Collections and its methods.

Examples:
In a default fresh Grav installation I’ve created 5 children below the home page:

user/pages/01.home
├── 01.child1
│   └── default.md
├── 02.child2
│   └── default.md
├── 03.child3
│   └── default.md
├── 04.child4
│   └── default.md
├── 05.child5
│   └── default.md
└── default.md

In template user/themes/quark/templates/default.html.twig, I’ve inserted the following snippet:

{% for child in page.children %}
   {{ child.title }},
{% endfor %}
  1. Output: Child 1, Child 2, Child 3, Child 4, Child 5,

  2. Slicing: Collection::slice($offset, $length)

    • Get the first 3 in the collection:
      {% for child in page.children.slice(0, 3) %} --> Child 1, Child 2, Child 3,
    • Get the last 3 in the collection:
      {% for child in page.children.slice(-3) %} --> Child 3, Child 4, Child 5,
  3. Sorting: Collection::order($by, $dir, $manual)

    • {% for child in page.children.order('date') %} --> Child 2, Child 1, Child 3, Child 4, Child 5,
  4. Chaining order + slice:
    {% for child in page.children.order('date').slice(0,3) %} --> Child 2, Child 1, Child 3,

Using Collection and its methods you can create (almost) any collection you want. Either to order and slice a dropdown menu and create a list of child pages.

  • Dropdown menu:
    Use user/themes/quark/templates/macros/macro.html.twig as an example on how a menu is created.
  • List of child pages:
    Play with above snippet in a (new) template and be creative with the output. The blog related templates are a good example for this.
2 Likes