Collections on $page

Should I be able do a collection on the current page, rather then $site?

A collection like this throws a Call to a member function images() on null

return function ($page) {
    return $page->images();

The reason I want to do this is because i am using a snippet to drive a featured images list on one page but i want to use the exact same snippet on other pages where it needs to use the current pages images instead. I tell it which collection to use as a variable on the snippet.

<?= snippet('albums/albumitems', ['set' => 'imagespage']) ?>

Why do you need a collection instead of simply calling $page->images()? There is no advantage whatsoever.

I knew you would say that :slight_smile:

It’s a complicated gallery and i need to use the collection about 5 times inside the snippet. Switching the collection used seemed slicker/lazier then writing some logic.

When the snippet is used on the other pages, its getting images from all of the subpages that are tagged as being featured, so for that i’ve used a collection. When you drill into a page, the same snippet does the same gallery but using the pages images, rather then the featured ones, so i just need a way to switch from the collection to the page.

I was just curious to know why I couldn’t do a collection on a page.

I’ve solved it like this…

$imagesrc = ($set == "page" ? $page->images() : $kirby->collection($set));

In collections you have access to the $kirby, $site, $users and $pages objects, these dependencies are injected. The current page object is not available, you can’t pass any arguments to the collection.

1 Like