Search code examples
twiggrav

Grav template -- Twig not listing child pages


I have made a few pages in grav with a taxonomy like this.

- Home (category type)
- programming (category type)
- stuff (category type)
    - stuff1 (page type)
    - stuff2 (page type)
    - stuff3 (page type)

I've also made a template type called "category" which should hopefully grab all the links to stuff1/2/3 and place them on the "stuff" page as links. My code looks a bit like this:

    {% block body %}
        {% block content %}
        <ul>
            {% for p in self.children %}
                <li><a href="{{p.url}}">{{ p.title }}</a></li>
            {% endfor %}
        </ul>
        {% endblock %}
    {% endblock %}

The end goal is to just get a simple listing of links for children to the category something like:

<ul>
   <li><a href="/stuff1">stuff1</a></li>
   <li><a href="/stuff2">stuff2</a></li>
   <li><a href="/stuff3">stuff3</a></li>
</ul>

I've tried using page.children, self.children, and a few other things but nothing seems to be getting this to work the way that I want it to.

Any help would be appreciated.


Solution

  • I'm afraid I do not quite understand what you mean by 'category type' and 'page type...

    The docs on Page Collections might be quite helpful.

    Example: If you want a page with template 'category.html.twig' which shows a list of urls to all pages containing a certain category (or tag), you can do the following:

    1. create a page 'category.md' defining a collection of categories in frontmatter:

      content:
        items:
          '@taxonomy.category': mycategory
      
    2. Create a template 'category.html.twig' containing:

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

    The resulting page will look like:

    <ul>
        <li><a href="/site-stack">Home</a></li>
        <li><a href="/site-stack/blog/hero-classes">Body & Hero Classes</a></li>
    </ul>