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:
books-folder
book1
book2
special-books-folder
specialbook1
book3
To collect the set of books I have a collection page containing:
content:
items: '@self.descendants'
To display the collection I was originally using this:
{% 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:
{% 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:
{% 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.