Using $file in Builder - Variable is not found

Hello there

i am using the Builder Plugin and i want to use “$file->exists()” in the head of my section to prevent Kirby from throwing errors on missing files.

I saw the error as i placed to Images with the builder and deleted accidently one image.

Why dont i have access on “$file” within the Builder section template?

Thank you

How is $file defined? Using exists() doesn’t make sense (because the method already requires an existing object), just check for $file(). Example:

$file = $page->files()->first();
if($file) {
  // do something
}

If this doesn’t help, please post your code.

<?php
// CHECK HERE 
if($data->picture() == '') return; ?>
<section class="row builder__image">
    <div class="columns">
        <figure class="popup">
            <img src="<?= $page->image($data->picture())->width(200)->url() ?>"
                 data-src="<?= $page->image($data->picture())->url() ?>"
                 class="responsive" />
            <noscript><img src="<?= $page->image($data->picture())->url() ?>"></noscript>
        </figure>
        <?php if($page->image($data->picture())->caption() != ''): ?>
        <p class="image-subtitle">
        <?= $page->image($data->picture())->caption() ?>
        </p>
        <?php endif; ?>
    </div>
</section>

On the Line #1 i want to check if the file which is in §data->picture() is physically on the server.
Because i can delete the files which are used in the Builder.

Thats causing an error:
Call to a member function width() on null

Thank you

if($image = $page->image($data->picture()):
// do stuff and use $image in the rest of the code so as not to repeat yourself
endif;
1 Like

Works, thanks!

I did it with “exists” a bit around the corner.