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

How to get URL of pages in a non-self collection

Started by Muut Archive 10 years ago · 4 replies · 320 views
10 years ago

I want to do list-and-detail in a page collection that isn't a self collection.

In a self collection it's easy enough, using a template containing this construction:

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

I can't see how to do it in the case I'm working with, where the collection header is:

TXT
    items: 
        '@page': '/PAGENAME'

At least, I can see how to do it if I hardwire the page name into the template, like this:

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

But surely page name shouldn't be hardwired into templates.

What's the right way to do it, please?

10 years ago

Try this:

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

Bingo again! You're good at this. Thank you!

10 years ago

In fact, I now realise I can use child.url in both cases, instead of the clumsier {{ page.url }}/{{ child.slug }}that I was using. Are there any disadvantages or hidden gotchas?

10 years ago

Nope, .url() is always the best approach as it takes into account things like route options in the page.

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