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)?
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()
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).