@page variable not working in Page collections

Hi all,

I am trying to build a vertical menu which would complement the horizontal menu with drop-down in the Deliver theme (more about that in Menu in Deliver theme).

Fot this purpose, I am using Page Collections, i. e. modified codes copied from https://learn.getgrav.org/16/content/collections

Out of the table Summary of collection options, those based on @root’, ‘@self and @taxonomy work fine (i. e. generate the list of topics). However, I need to take advantage of the @page functionality and that is not working.

This is the frontmatter of the relevant page:

content:
    items:
        '@page': '/software'
    order:
        by: name
        dir: asc
    pagination: false

and this is the code for generating the menu:

            <ul>
                    {% for post in page.collection %}
                    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
                    {% endfor %}
            </ul>

What am I doing wrong?
THX!

@sue, Using a default fresh Grav installation, I did the following which seems to work as expected…

  • I created the following folder structure:
    pages
    ├── 01.home
    │   └── default.md
    ├── 02.typography
    │   └── default.md
    └── software
        ├── software1
        │   └── default.md
        └── software2
            └── default.md
    
  • In file ‘02.typography/default.md’ I created a collection by copying your code:
    ---
    title: Typography
    content:
        items:
            '@page': '/software'
        order:
            by: name
            dir: asc
        pagination: false
    ---
    
  • And copied your loop to ‘themes/quark/templates/default.html.twig’
    {% extends 'partials/base.html.twig' %}
    
    {% block content %}
        <ul>
            {% for post in page.collection %}
                <li><a href="{{ post.url }}">{{ post.title }}</a></li>
            {% endfor %}
        </ul>
    
        {{ page.content|raw }}
    {% endblock %}
    

And this is the result when I browse to page ‘typography’:

So, it seems your code and collection definition are OK…

How does your folder structure look like? Is ‘/software’ located directly underneath ‘/pages’?

Hi, yes, the /software is located directly underneath the /pages. The loop is in
themes/deliver/templates/partials/sidebar.html.twig.

Thanks.

@sue, Also in ‘templates/partials/sidebar.html.twig’ from theme Deliver, the collection is shown correctly…

  • Are you sure the loop is executed?
  • Would you mind sharing the html output of:
    {{ var_dump(count(page.collection)) }} 
    <ul>
        {% for post in page.collection %}
            <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
    </ul>
    
  • Would you mind sharing your folder structure?
    • To which ‘relevant’ page did you add the collection definition?
    • In which page do you expect to see the list?

Hi, a funny thing happened: the variable /filename did not work and nothing improved over the weekend. I decided to give a shot to default.html.twig, so I copied the loop there, reloaded the page… and it was working! Both in the sidebar.html.twig and the default.html.twig!
Although I do not understand why it started working, I am happy that it did :wink: