Hi!
There’s one thing about Kirby that always gets me because I expect it to be super easy to do with Kirby’s API, but when I get around to building it, it turns out to be not quite as simple:
Working with collections of files and images, especially across several pages and when they have to be merged.
Today, it took me quite a lot of googling and searching in this forum to figure out how to get a collection of all files in a page, including those of its children (& their children, & theirs…)
It’s extremely easy to get one level of files from a page: $page->files()
, and if you want to output files in the order they’re nested, it’s no problem to loop over children (or index) of a page to do this.
But if you’re after a big collection of files that you can filter, search, limit or shuffle independently, there’s not much in Kirby’s API that helps you with that.
Eventually, the following two forum topics helped me finish my task, but the current solution breaks some beloved functions like Kirby’s $files->shuffle()
and is not documented outside the forum.
So, to wrap this up: It would be great if dealing with multi-level file/image collections was possible in a more Kirby-ish way.
Some suggestions:
- There could be something like
$pages->files()
, which would return all files from the pages collection. Then it would be easy to get all nested files or images of any page like this:$page->index()->files()
. - Easier merging & adding of collections, for example like so:
$images->add($moreImages)
Another note:
The cheatsheet entries for $site->images()
, $site->files()
etc. are so short that they can easily be mis-understood:
Returns a $files collection with all files in /content
It’s quite easy to wrongly assume this to mean that it returns all files within /content, even those nested in subpages. Obviously it’s not what it does, so a more specific description would be welcome.
Thanks!
PS: Actually, this should probably extend to pages collections as well. For example, $pages->add()
is not in the cheatsheet, and the stuff discussed in Showing all posts from different folders (merging collections) - #8 by texnixe could also be kirbyfied somehow (merge($pages, $morePages)
) & would deserve a mention in the cheatsheet.