Next/Prev page vs Collections

Hey Guys,

can you explain actual behavior of next/prev.
Probably I do not understand the concept of collection or I’m doing something wrong.
Functions like nextSibling still not working for me.

My pages are marked as @self.sibling.
Screenshot 2016-01-25 12

I’m able to list them by page.collection loop.

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

But simple task as page.next or page.nextSibling or page.collection.next or page.collection.nextSibling still not work for me. Where the trouble is?

Can you share small snippets of template how to use that?

Native collection @self.siblings excludes current page from the collection (according to docs). That is propably reason, why at least these methods are not working: page.isFirst(), page.isLast(), .page.prevSibling(), .page.nextSibling().

The workaround is to set another collection in the template and use that one instead of the native collections.

{% set groupofpages = page.collection({‘items’:{’@page’: ‘/portfolio/tables’}}) %}

That makes listing of pages in current level in folder structure a bit complicated. Hopefully someone has idea how to solve this?

The Learn site has an example usage: https://github.com/getgrav/grav-theme-learn2/blob/develop/templates/docs.html.twig#L6-L19 which creates a Collection and iterates over it.

page.isFirst(), page.isLast(), page.prevSibling(), page.nextSibling() work when the page is part of a collection.

If you see their implementation, they simply lookup the parent page, and they call the respective methods on the Collection itself.

Heh, that’s wierd. Why to do things easy if it is possible to do them difficult.

I want only simple link to next page.

What the above means is that you need to ask the Collection.

{% set collection = page.collection %}
{% for child in collection %}
   {{ dump(collection.isFirst(child.path))}}
   {{ dump(collection.prevSibling)}}
    <div><a href="{{ child.url }}"> {{child.title}}</a></div>
{% endfor %}

The Page object are convenience methods that lookup the parent collection, as that’s what usually happens in a blog for example, see https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Page/Page.php#L1759-L1820

This doesn’t work for me. it is causing exception

when I comment out dumps it show me sibling pages, but I cant go to next page (+1) or previeous page (-1)

hmmm… I dont thing that solves the problem of the @self.siblings collection. I have solved it like this, but it is not perfect as it is not universal:

{% set folder = "/portfolio/"~page.parent.slug %}
{% set recollection = page.collection({'items':{'@page': folder},'order': {'by': 'default', 'dir': 'asc'}}) %}

Than isFirst() and nextSibling() etd. works correctly for folder structure (just one level):

{{recollection.nextSibling(page.path).title}}
{{recollection.prevSibling(page.path).title}}

If native methods like .nextSibling() would work correctly on folder,that would be much cleaner…

@lexyk yep, this is way if you know your path and slug. That’s funny I’m trying solve so easy task almost one day.

Yep sorry I was misinformed on @self.siblings. As @lexyk says, that collection returns the siblings without the current page, so the collection does know know at which point it is in the current collection, as it does not contain the current page…

Your best bet is using what he suggested, get the childs of the parent page. Which is what the Cacti blog does for example, in a slightly different way, by setting the collection on the parent page https://github.com/getgrav/grav-skeleton-multilang-site/blob/develop/pages/01.blog/post-list.en.md https://github.com/getgrav/grav-theme-cacti/blob/develop/templates/post.html.twig#L27-L39

Hey guys, Thanks. But I’m sorry. This is not working for me again. I have no parent pages for these pages. All of them are root pages. I tried cacti approach, but without luck.

Otherwise:
What happen when I set root page (md file, content:items:) to @root and then I will need move this page from root somewhere deeper into the web structure. That mean I need care about this. And just rewrite @root to @self.childern or whatever?

This is working pretty fine for taxonomy (I did not test taxonomy so much).
But when core doesn’t know what is next page on same level, without this kind of dependency linking. Especially on flat file system. It is wierd for me.
I’m really sorry for telling this. But I did not expect I will spend couple of hours
on this basic task.

Just now I will try patch the core Page.php class and add @self.allsiblings which
will contain all pages on current level. Understand this is pretty dirty.
But is is the easiest solution of my problems. This kind of collection is missing to me.

Screenshot 2016-01-25 20

If you really feel you need this, submit a Pull Request to add this method. We are not averse to adding functionality that people need.

Understand, that is not problem crete pull request. But first I will test patch at all. I need to be sure it is not not causing some unexpected problems.