Access pagination object inside collection item

Hi all,

I’m working on a blog site and am seeing a problem.

I use a controller for the blog page, which gets all child pages (blog articles), sorts and paginates them. It then hands the articles and pagination objects to the blog template.

So far so good, it’s nearly identical to the examples in the Kirby docs. The problem comes when I want to add pagination elements inside a blog article: I want to show links for “next/previous article” as well as an index “article X of N” at the end of every article.

What I’ve found in the docs, the starterkit and here, is using $page->nextListed() and $page->prevListed() to get those items. However, these methods work on the entirety of all blog articles, not on the collection that was initially returned by the blog controller. So if the sorting there was different or there was a filter active, that would not apply here.

So my question is: What is the best way to access the pagination object that was created in the blog controller, from inside the article template (or the article controller)?

Thanks and Regards,
Jere

Welcome to the community :slight_smile:

I would probably use Collections for this which you will be able to feed into both the controller for the blog listing and the individual articles. You can do the filtering / sorting etc that you need inside the collections definition.

Infact you probably dont need to put it throught the controller, you can just feed the collection into the for loop rather than $page->children()->listed()-flip()

Missed that part… you can use count() on the collection and index of on the collection to get those numbers.

In those example $collection is what is returned by your collection

Do this in the article controller and pass it out to the template (replace the collection name with whatever you called it):

$index = $kirby->collection("blogArticles")->indexOf($page):
$count = $kirby->collection("blogArticles")->count();

Thanks so much @jimbobrjames! That was exactly what I needed (and to be honest, should have stumbled upon myself in the docs…)!
Using a collection I don’t need the pagination item, since i can get the next/previous item directly from the collection. Therefore I create the pagination only in the blog controller and not in the collection (which is a good idea anyway, as the docs suggest).

1 Like

Glad i helped :slight_smile: