How to make recursive list-and-detail work

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.

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

This seems to be an effective answer:

{% 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.

Let us know if any more problems persist :slight_smile: