Filtering images by template does not work

This is my first Kirby project, so if you have any suggestions about how to filter/handle images, I am open to any kind of feedback. My project structure and the problem I am facing is as following:

I have dozens of pictures on my Homepage and need to filter these so that all pictures aren’t displayed in one files field. So I used file templates to group these pictures. They are successfully displaying on their own files fields.

Every image template group is numbered from 1_ on. This means that I have several image groups which have their own template, e.g. “home_background_image”, and they are numbered like mentioned. That is why I am using nth() to access images. They are numbered and each number will correspond to a specific place in my Home.php. For example, number 3 with the home_background_image template will always display as the third background image, even if the client replaces the image with a different name. What matters is the number 3_

The problem is, in my Home.php, I cannot filter these image according to template:

$page->images()->filterBy('intendedTemplate', 'home_background_image')->nth(1)->url()

Does not work. I tried many different versions of this, but filterBy seems to return null in every single case I tried. Can somebody help me with this? When clicking on the image in the panel I see the file fields, so the templates work. My homepage_background_image yaml is:

title: Homepage Background Image

fields:
  alt:
    label: Alt Text
    type: text

Each image has a corresponding .txt file (1_home.jpg has 1_home.jpg.txt) with the simple following line:

template: homepage_background_image

Any feedback is greatly appreciated.

Images don’t have an intendedTemplate, just a template, so

$images = $page->images()->template('sometemplate');

which is short for

$page->images()->filterBy('template', 'home_background_image')
1 Like

Edit: The issue I mentioned in this reply turned out to be a typo. Your answer solved my problem, many thanks!

I have the same problem in my query option in my files section:

homeimagery:
    type: files
    query: page.images.template('homepage_background_image')

Does not work, but simply adding the option

homeimagery:
    type: files
    template: homepage_background_image

works. I need the query because I want to use nth() to fetch only one image in some fields.

I tried changing the syntax by using the site object instead but had no luck. Any idea why this doesn’t work?

What is homeimagery, a files field or a files section? A files section doesn’t have a query property while a files field has.

1 Like

Oh, that clears things up! It’s a files section.

I switched to using the files field for making the client choose a single image. Im sticking to the files section for uploads though, as I want my templates applied to them. Shame that we can’t do the same for the files field uploads, I have to disable it then.

Thanks for your help, I was stuck for hours!

You can very well assign a template when uploading files via a files section, only you have to do this inside the uploads property:

1 Like

Can’t believe that I missed it, this is exactly what I wanted! Thank you :slight_smile: