Outputting image name in Kirby3

I have encountered a problem with kirby3 and outputting image names in the frontend.

in my blueprint I have setup the following files field:

  label: Voransicht Bild
  type: files
  help: Das Bild, welches auf der "/news" Seite als Vorschau angezeit wird.
  max: 1

In my Template I am trying to output the File name with the following:

<?php echo $page->preview_image();?>

In my Frontend I get this result: “- sample.jpg” even though the image is just called “sample.jpg”.
Why is the dash and space also attached to the filename?

You will have to convert the files field value to an image object first.
If you have a look at the content file you will see something like this:


- sample.jpg

Looking at that, the echo result won’t be a surprise anymore.

You can do something like the following:

<?php if($image = $page->preview_image()->toFile()): ?>
  <?= $image->filename() ?>
<?php endif ?>

If you need more than just the filename use $image->url().

FYI: There’s a spelling error in the help text, it should be “angezeigt.” :point_up::wink:

1 Like