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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

@page variable not working in Page collections

Solved by pamtbaau View solution

Started by Zuzana Fenclová 6 years ago · 4 replies · 785 views
6 years ago

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 /forum/themes-styling/menu-in-deliver-theme-t6666?u=sue).

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:

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

and this is the code for generating the menu:

<div class="sidebar-content">

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

What am I doing wrong?
THX!

6 years ago

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

  • I created the following folder structure:
    TXT
    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:
    YAML
    ---
    title: Typography
    content:
      items:
          '@page': '/software'
      order:
          by: name
          dir: asc
      pagination: false
    ---
    
  • And copied your loop to 'themes/quark/templates/default.html.twig'

    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':
image|690x152

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

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

6 years ago

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

Thanks.

6 years ago Solution

@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:
    TWIG
    {{ 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?
last edited 02/14/20 by pamtbaau
6 years ago

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 ;)

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 76 8 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago