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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Links to sibling page in collection – closing the loop

Solved by pamtbaau View solution

Started by John Martyn 4 years ago · 3 replies · 495 views
4 years ago

Hi everyone!

A pretty simple question.

I have a collection page (say for 'animals'), and its children pages ('pelican', 'lion', etc).

I'm trying to add next/page links to sibling pages in a same collection. I put together this, which seems to work fine:

TWIG
{% if page.isLast(page.path) == false %}
  <a href="{{page.prevSibling(page.path).url }}">next</a>
{% endif %}
{% if page.isFirst(page.path) == false %}
  <a href="{{page.nextSibling(page.path).url }}">prev</a>
{% endif %}

My question is: if the current page is, say, the first in the collection ('Antelope'), how can I add a rule so that prev links to the last page in the collection (say, 'West Highland Terrier')? And vice versa.

Cheers,

4 years ago Solution

@PurpleRoom,

which seems to work fine

Yes, that seems to work fine, but only because PHP is so forgiving and doesn't throw errors on incorrect code...

Class Page (see docs) does not have a function Page::isLast(string $path), only Page::isLast(). PHP will happily ignore the parameter.

Back to the question...

Looking at the implementation of Page::isFirst(), it will do the following:

PHP
public function isFirst()
{
   $parent = $this->parent();
   $collection = $parent ? $parent->collection('content', false) : null;
   if ($collection instanceof Collection) {
        return $collection->isFirst($this->path());
   }

   return true;
}

Look at the docs about Collections you will find the following:

|Collection::first()|Get the first item in the collection|
|Collection::last()|Get the last item in the collection|

When combining the two, when getting the collection of the parent, you can also get the first and last element in the collection.

TWIG
{{ page.parent.collection.first.url }}
{{ page.parent.collection.last.url }}

Note:

  • I think you want to create some circular looping through siblings. I don't know your use-case, but I'm afraid this is confusing to the end-user. When will the end-user come to the conclusion that he/she is looping?
  • It might be that pagination might throw of the first and last item in the collection. Just try and see what happens.
👍 1
last edited 06/25/22 by pamtbaau
4 years ago

Yes, that seems to work fine, but only because PHP is so forgiving and doesn’t throw errors on incorrect code…

😅

Dear @pamtbaau, you are the forgiving one!

In all seriousness, many, many thanks for taking the time for this excellent explanation, and pointing me to the correct solution.

A great day to you!

4 years ago

Oh and just to answer on the side notes:

I think you want to create some circular looping through siblings. I don’t know your use-case, but I’m afraid this is confusing to the end-user. When will the end-user come to the conclusion that he/she is looping?

You are right to raise the issue. In this case the correct succession of pages is not meaningful or important, so it's more about providing a convenient navigation.

It might be that pagination might throw of the first and last item in the collection. Just try and see what happens.

Looks perfect!

last edited 06/25/22 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 10 hours ago
General · by pamtbaau, 15 hours ago
1 51 15 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago