Please help to show image

Hi, I have section with sponsors, and there is field for uploading image (logo):

title: Sponsor

status:
  draft: Draft
  listed: Published

tabs:
  pages:
    label: Page
    icon: page
    columns:
      - width: 2/3
        sections:
          content:
            type: fields
            fields:
              featured:
                type: checkboxes
                options:
                  front: Rodyti pirmame puslapyje
              nuoroda:
                type: url
              text:
                type: textarea
                size: small


      - width: 1/3
        sections:
          logo:
            type: files
            layout: cards
            template: image
            max: 1

Can’t understand, how to show images on the page, that shows all sponsors, or on front page, where promoted sponsors are shown. Here is what I tried:

  <?php if($img = $sponsor->logo()->toFile()): ?>
  <div class="card-inner card-image">

      <figure class="image" role="group">
        <?= $sponsor->$img()->Resize(300, 300) ?>
      </figure>
  </div>
  <?php endif ?>

Here is how it looks in panel:

Your on the right track. Im assuming these logos are all in sub pages, so you just need to loop through the subpages, and check the featured field in a filter. Without knowing the structure of the site though, it’s hard to give you an exact code example. You do have a small error in your code though.

This is wrong:

<?= $sponsor->$img()->Resize(300, 300) ?>

Should be:

<?= $img->resize(300, 300) ?>

To loop through them, you would need something like this:

<?php foreach ($site->find('sponsors') as $sponsorimg): ?>
  <?php if($img = $sponsorimg->logo()->toFile()): ?>
  <div class="card-inner card-image">
      <figure class="image" role="group">
        <?= $img->resize(300, 300) ?>
      </figure>
  </div>
  <?php endif ?>
<?php endforeach ?>

There are a couple of ways to check the value of the checkbox. Id probably go with filterby() on the for each loop.

The problem is that your logo thingy is not a files field but a files section. So it doesn’t store anything in your content file but only uploads the file and stores the selected template (in this case image) in the file’s meta data.

Consequently, you can’t fetch the file with <?php if($img = $sponsor->logo()->toFile()): ?>but have to get it like this:

<?php if ($img = $sponsor->files()->template('image')->first()): ?>
<div class="card-inner card-image">
  <figure class="image" role="group">
    <?= $img->resize(300, 300) ?>
  </figure>
</div>
<?php endif ?>

This fetches all files, filters them by the image template and then gets the first.

The alternative would be to add an additional files field where you store the image name, but that doesn’t really make sense in this case. In the future, when we have a files field with upload possibility (probably v 3.2), that will be the way to go.

texnixe, thank you very much, it helped and now works!

jimbobrjames, thank you, with foreach all is ok, just didn’t include that part here.