Images being returned as blank in the $page object?

Hi,

I’m using Kirby to set up a webcomic site, and I’m running into an issue when trying to get the image files that are defined in my blueprint:

title: Comic
icon: 🖼

status:
  draft: true
  listed: true

columns:

  # main content
  main:
    width: 1/3
    sections:
      thumbnail:
      	type: files
      	headline: Thumbnail
      	template: thumbnail-image
      	min: 0
      	max: 1
      	image:
      	  cover: true
      mainimage:
      	type: files
      	headline: Main horizontal image
      	template: main-image
      	min: 1
      	max: 1
      	image:
      	  cover: true
      midimage:
      	type: files
      	headline: Mid-sized image (for tablets)
      	template: mid-image
      	min: 0
      	max: 1
      	image:
      	  cover: true
      stackedimage:
      	type: files
      	headline: Stacked image (for very narrow screens)
      	template: stacked-image
      	min: 0
      	max: 1
      	image:
      	  cover: true
      imagemeta:
        type: fields
        fields:
          alt:
            label: Alt text
            required: true
            type: text
          tags:
            label: Tags
            type: tags
          allowcomments:
            label: Comments
            type: checkboxes
            options:
              true: Allow comments on this comic/post?

  # meta information
  sidebar:
    width: 2/3
    sections:
      content:
        type: fields
        fields:
          date:
            label: Publish date/time
            type: date
            time: true
            default: now
            required: true
          transcript:
            label: Transcript
            type: textarea
            size: small
          blog:
            label: Blog post
            type: textarea
            size: large

When loading the page via my comic.php template file, the images are all shown as entries in the $page object (e.g. $page->mainimage()), but they’re blank. I could go down the $page->files() route to get them, but then I’d lose the associative names.

Am I doing something wrong with the template?

Could you post your comic.php template please, so that we can see how you are trying to fetch the images?

In general, you can filter the pages files by the template assigned to them in the blueprint, e.g.

For multiple images

$midimage = $page->files()->template('mid-image');
$mainimage = $page->files()->template('main-image');

These example will return a files object (a collection).

Since your file sections are all set to max 1, it’s better to fetch them by template name

if ($midimage = $page->files()->findBy('template', 'mid-image')):
  echo $midimage->url();
endif;

The whole template is a bit much to post here, but by doing:

<?= $page->mainimage() ?>

Or

<?php print_r($page->content()); ?>

I can see that those variables are all blank in the $page array.

I’ll give the methods you mentioned a try.

You are using sections, and sections don’t store anything in the content file, therefore your code doesn’t work. See my post above for a solution.

If you want to store those file associations in the content file, you’d have to use the files field instead, but currently it doesn’t allow uploading of files (planned for 3.2).

Thanks for the info (can you tell I’ve not been using Kirby that long?). I used your solution, and I’ve got everything working now. Thanks!