Logo's not shown

Hi,

For some reason my code don’t output my logos (images). The H3 is outputted correctly. Maybe I don’t see it. Can anybody help me?

        <div class="row mar-top-100">
            <div class="col-sm-3">
                <h2>Clients</h2>
            </div>
            <div class="col-md-9 col-sm-12 clients-list">
                <div class="row">
                    <?php foreach($clients = $site->find('clients')->children()->listed()->sortBy('date', 'asc')->flip()->limit(4) as $client): ?>
                    <div class="col-sm-3">
                        <h3><?= $client->title()->html() ?></h3>
                         <?php if($image = $client->logo()->toFile()): ?>
                        <img src="<?= $image->crop(200, 200)->url() ?>" alt="">
                        <?php endif ?> 
                    </div>
                    <?php endforeach ?>
                </div>
            </div>
        </div>

Blueprint:

title: Client
preset: page
num: zero

status:
  draft: true
  listed: true

fields:
  tags:
    type: tags
    label: Tags
    options: query
    query: page.siblings.pluck("tags", ",", true)
  link:
    type: url
  description:
    type: textarea

sidebar:
  logo:
    extends: sections/clientlogo
    template: cover
    min: 1
    image:
      ratio: 1/1

Thanks!

You are not using a files field by a files section, so you can’t call that as if it was a field.

$images = $client->images()->template('cover');
foreach ($images as $image) {
  // do stuff
}

On a side note, setting the $clients variable in this line in the foreach loop:

<?php foreach($clients = $site->find('clients')->children()->listed()->sortBy('date', 'asc')->flip()->limit(4) as $client): ?>
                   

seems pretty useless and I doubt this is considered good coding style. If you want to use the $clients variable, define it in your controller or before the loop in the template:

$clients = $site->find('clients')->children()->listed()->sortBy('date', 'asc')->flip()->limit(4);
foreach ($clients as $client) {
  // ...
}

and you have much cleaner code.

So if I get it right.

<div class="row mar-top-100">
    <div class="col-sm-3">
        <h2>Clients</h2>
    </div>
    <div class="col-md-9 col-sm-12 clients-list">
        <div class="row">
            <?php $clients = $site->find('clients')->children()->listed()->sortBy('date', 'asc')->flip()->limit(4);
foreach ($clients as $client) {
            <div class="col-sm-3">
                <h3><?= $client->title()->html() ?></h3>
                 <?php 
$images = $client->images()->template('cover');
foreach ($images as $image) {
  <img src="<?= $image->crop(200, 200)->url() ?>" alt="">
}}

            </div>
            <?php endforeach ?>
        </div>
    </div>
</div>

edit: oh this is not working.

There’s an endif that shouldn’t be there…

Can’t get it working :sweat_smile:

Could you provide some more information what exactly is not working? do you get an error message? Or no files are shown? Or something else?

Edit: Ah, I just realized that you are using curly braces where you can’t

                 <?php 
$images = $client->images()->template('cover');
foreach ($images as $image) {
  <img src="<?= $image->crop(200, 200)->url() ?>" alt="">
}}

this code will not work.

Somehow I don’t get it working. Got a syntax error. But I think I don’t get your help the right way.

<div class="row mar-top-100">
    <div class="col-sm-3">
        <h2>Clients</h2>
    </div>
    <div class="col-md-9 col-sm-12 clients-list">
        <div class="row">
            <?php $clients = $site->find('clients')->children()->listed()->sortBy('date', 'asc')->flip()->limit(4);
foreach ($clients as $client) {
            <div class="col-sm-3">
                <h3><?= $client->title()->html() ?></h3>
                 <?php 
$images = $client->images()->template('cover');
foreach ($images as $image) {
  <img src="<?= $image->crop(200, 200)->url() ?>" alt="">
}}

            </div>
            <?php endforeach ?>
        </div>
    </div>
</div>

If you mix HTML and PHP you can’t use curly braces the way you do, didn’t know you were not familiar with PHP syntax.

<div class="row mar-top-100">
  <div class="col-sm-3">
    <h2>Clients</h2>
  </div>
  <div class="col-md-9 col-sm-12 clients-list">
    <div class="row">
      <?php 
      $clients = $site->find('clients')->children()->listed()->sortBy('date', 'asc')->flip()->limit(4);
      foreach ($clients as $client) : ?>
        <div class="col-sm-3">
          <h3><?= $client->title()->html() ?></h3>
          <?php
          $images = $client->images()->template('cover');
          foreach ($images as $image) : ?>
            <img src="<?= $image->crop(200, 200)->url() ?>" alt="">
          <?php endforeach ?>

        </div>
      <?php endforeach ?>
    </div>
  </div>
</div>
1 Like

Hi @texnixe,

Yes now its works and I do understand what I did wrong.

I am sorry I have so little knowledge. But you just learned me something. Thanks. Thats great!

Thanks for your awesome support!

You are welcome.

I’m sometimes a bit lazy when typing examples and not always fully aware how much people can abstract from what I’m posting. And I could have spotted the error earlier.

1 Like