Output 'files' array as url's

I have an array of images that I’d like to output the URL’s of. I’m using;

    <?php foreach($data->gridImages() as $items): ?>
      <div class="grid__image">
        <?php dump($items) ?>
    <?php endforeach; ?>

Which outputs this:

    [0] => erasmus.png
    [1] => may-ninth.png

How do I turn these into actual files? I’ve tried a lot of variations for the past hour or so and can’t crack it.

I’ve also tried;

    <?php foreach($data->gridImages() as $items): ?>
      <div class="grid__image">
        <?php foreach($items as $item): ?>
          <?php dump($item) ?>
        <?php endforeach; ?>
    <?php endforeach; ?>

But if I try something like toFile() I get an error warning me about strings.

Is that content from a files field? => https://getkirby.com/docs/reference/panel/fields/files#how-to-use-in-templates-snippets

Yeah, it’s via this part of the blueprint;

          type: files
          headline: Available Images
          layout: list
          info: "{{ file.dimensions }}"
          template: image

Because I’m using the ‘builder’ plugin, I’m using this section to upload a bunch of images that can then be placed where you like using the following blueprint;

label: Grid
    type: files
    label: Included Photographs
    layout: cards
    label: Captions
    type: toggle
    text: Show captions next to each image?
    width: 1/2
    label: Grid Background Colour (optional)
    type: color
    editableAlpha: false
    default: "#efefef"
      - "#efefef"
    width: 1/2

Using this plugin: https://github.com/TimOetting/kirby-builder

You can fetch images from a files field using the toFiles() method as explained in the docs I linked to:

  <?php foreach($data->gridImages()->toFiles() as $file): ?>
     <div class="grid__image">
         <?php echo $file->url() ?>
   <?php endforeach; ?>

Echoing the url here without an image doesn’t make much sense, so you would have to adapt your HTML or just echo $file.


The toFiles() function was a big one. I was using toFile() in the same way but getting an error, didn’t realise I just needed to pluralise :slight_smile:

For a files field that is limited to having only one file, toFile() makes more sense, because toFiles() gives you a collection, whereas toFile() returns a single file object if it exists.

1 Like