$site->images(), doesn't work?

Hi,

I’m trying to make make a full list of images that are in the /content.
This example for $site->images() seems to be doing exactly that, yet it doesn’t show anything.
Can anyone guess why that would be?

The $page->images() works fine everywhere, so I don’t think there’s anything particularly wrong with the images themselves or the structures around it.

The code I’m using is not different than the example, but just in case you can look at my images.php template at github.

I would appreciate any clue to solve this issue. Thanks!

$site->images() only fetches image files at the root of your content folder (uploaded to the “Site Options” area of the Panel).

You may need to loop over each page in $site->index(), then output the images for each of them. as far as I know, there is no way to directly fetch all images from a collection of pages.

That’s also how I would do it. Example code:

foreach($site->index() as $p) {
  foreach($p->images() as $image) {
    // Do stuff with the image
  }
}

Thanks @AugustMiller @lukasbestle! My understanding of $site->images() was was apparently wrong.

I just had the same issue with $site()->documents(). I had no idea it was just /content and not its subfolders.

Should the docs be improved to specify this? It currently reads

Returns a $files collection with all documents (pdf, doc, xls, etc.) in /content

It’s sorta ambiguous.

In the latest Kirby update, $pages->files() was introduced, so you could use that for now. (It’s not documented yet, but it simply gets all files of a pages collection, so you can do $site->index()->files())

Maybe there should also be $pages->images(), or is there already a super straightforward way to filter all images from the collection returned by $pages->files()?

I think so, I was confused by this as well.

$pages->files()->filterBy('type', 'image')

Respectively:

 public function images()    { return $this->files()->filterBy('type', 'image');    }
 public function videos()    { return $this->files()->filterBy('type', 'video');    }
 public function documents() { return $this->files()->filterBy('type', 'document'); }
 public function audio()     { return $this->files()->filterBy('type', 'audio');    }
 public function code()      { return $this->files()->filterBy('type', 'code');     }
 public function archives()  { return $this->files()->filterBy('type', 'archive');  }
1 Like

There we go, thanks very much! :slight_smile:

For the sake of consistency, it might be worth making these public functions work for $pages as well, not just $page.

2 Likes

Thanks for the feedback. I’ve just improved the wording of the $site docs.

2 Likes