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

How to make recursive list-and-detail work

Started by Muut Archive 10 years ago · 3 replies · 356 views
10 years ago

I have run into two problems with a recursive page collection. I have found possible solutions for both, but I would be grateful to be told if there are better solutions.

My folder structure is:

TXT
books-folder
    book1
    book2
    special-books-folder
        specialbook1
    book3

To collect the set of books I have a collection page containing:

YAML
content:
    items: '@self.descendants'

To display the collection I was originally using this:

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

That works fine for book1, book2, book3, ...., generating a link with address /books-folder/book1.

But I have two problems.

Problem 1

That version also lists special-books-folder, with the title "Special Books Folder". Obviously I don't want to list it. My solution:

TWIG
    {% for child in page.collection %}
        {% if child.header.content is defined %} {# ignore - don't list folder names #}
        {% else %}

Problem 2

The previous version generates bad links for second level books such as specialbook1, generating a link with the address /books-folder/special-books-folder/book1. My solution:

TWIG
    {% for child in page.collection %}
        <a href="{{ page.route }}/{{ child.slug }}</a>
    {% endfor %}

Are these solutions the best ones? Are there cases where they fail?

Many thanks for your help.

10 years ago

Oops. I now see I have NOT solved Problem 2. For some reason I thought I had. Any offers?

10 years ago

This seems to be an effective answer:

TWIG
{% for child in page.collection %}
    <a href="{{ child.route }}/style:style1">{{ child.title }}</a>
{% endfor %}

So I think I've solved it. At least, I think I have.

10 years ago

Let us know if any more problems persist :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2955 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago