# Testing for images by filename

**URL:** <https://forum.getkirby.com/t/testing-for-images-by-filename/17048>\
**Category:** Questions\
**Tags:** v3\
**Created:** [January 30, 2020, 10:39pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048 "2020-01-30T22:39:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Olsen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/erik_olsen/32/6874_2.png) [@Erik\_Olsen](https://forum.getkirby.com/u/Erik_Olsen)\
**Post date:** [January 30, 2020, 10:39pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/1 "2020-01-30T22:39:59Z")

</div>

I’m trying to include a slider on my posts page, if there are images with “slider” in the filename. I can get the code to work, but my “if” statement doesn’t. What am I missing?

```
<?php if ($slider = $page->images()->filterBy('filename', '*=', 'slider')): ?>
<div class="slider" data-arrows="true">
	<ul class="slides">
		<?php foreach ($slider as $image): ?>
		<li>
			<img alt="Image" src="<?= $image->url() ?>" />
		</li>
		<?php endforeach ?>
	</ul>
</div>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 30, 2020, 10:50pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/2 "2020-01-30T22:50:48Z")

</div>

You can use `isNotEmpty()` to test if a collection has items:

```auto
<?php 
$slider = $page->images()->filterBy('filename', '*=', 'slider');
if ($slider->isNotEmpty()): ?>
<div class="slider" data-arrows="true">
	<ul class="slides">
		<?php foreach ($slider as $image): ?>
		<li>
			<img alt="Image" src="<?= $image->url() ?>" />
		</li>
		<?php endforeach ?>
	</ul>
</div>
<?php endif ?>

```

With your if statement, you only test if the collection exists, but this will return true because the collection exists even if it has no items.

---

<div class="post-metadata">

**Author:** ![Erik\_Olsen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/erik_olsen/32/6874_2.png) [@Erik\_Olsen](https://forum.getkirby.com/u/Erik_Olsen)\
**Post date:** [January 30, 2020, 10:53pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/3 "2020-01-30T22:53:35Z")

</div>

Makes total sense. I was trying to add hasImages() at the end and wasn’t successful with that either.

---

<div class="post-metadata">

**Author:** ![Erik\_Olsen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/erik_olsen/32/6874_2.png) [@Erik\_Olsen](https://forum.getkirby.com/u/Erik_Olsen)\
**Post date:** [January 30, 2020, 10:55pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/4 "2020-01-30T22:55:36Z")

</div>

Is it possible to add this filter into a collection?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 30, 2020, 10:57pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/5 "2020-01-30T22:57:21Z")

</div>

Yes, you can totally do that. But you cannot pass a page as a variable to a collection. The better option would be a page model or a controller.

---

<div class="post-metadata">

**Author:** ![Erik\_Olsen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/erik_olsen/32/6874_2.png) [@Erik\_Olsen](https://forum.getkirby.com/u/Erik_Olsen)\
**Post date:** [January 30, 2020, 10:58pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/6 "2020-01-30T22:58:53Z")

</div>

Awesome. I’m just trying to wrap my head around the best way to reduce the code in the template.

So I’ll build a collection looking for those images. Then I can load those images in my post.php controller. Do I test for the existence in the controller or on the post.php template?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 30, 2020, 10:59pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/7 "2020-01-30T22:59:56Z")

</div>

See my edit above.

---

<div class="post-metadata">

**Author:** ![Erik\_Olsen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/erik_olsen/32/6874_2.png) [@Erik\_Olsen](https://forum.getkirby.com/u/Erik_Olsen)\
**Post date:** [January 30, 2020, 11:51pm UTC](https://forum.getkirby.com/t/testing-for-images-by-filename/17048/8 "2020-01-30T23:51:06Z")

</div>

For anyone watching, I ended up creating a “post” controller and this code to load the slider images:

```
return function($page) {
$slider = $page->images()->filterBy('filename', '*=', 'slider');

return compact('slider');
}
```
