isNotEmpty() not working (?)

I have a basic template that includes a slider. Each slide has an image, and (optionally) link and a description (also optional).

This is the template:

<?php snippet('header') ?>
 
<article class="slider-container swiper">
  <div class="swiper-wrapper">
    <?php foreach($page->slides()->toBlocks() as $slide): ?>
      <div id="<?= $slide->id() ?>" class="swiper-slide slide slide-type-<?= $slide->type() ?>">
        <?php if ($slide->link()->isNotEmpty()): ?>
        <a href="<?= $slide->link()->toUrl() ?>" class="slide-link">
          <?= $slide ?>
        </a>
        <?php else: ?>
          <?= $slide ?>
        <?php endif; ?>
      </div>
    <?php endforeach; ?>
  </div>
  <!-- pagination -->
  <div class="swiper-pagination"></div>

  <!-- navigation -->
  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>

</article>

<?php snippet('footer') ?>

However, the link is not displayed even when I have entered it.

This is the template for the slide:

<figure>
  <?php $image = $block->image()->toFile();
  snippet('image', 
    [
      'image' => $image,
      'targetUse' => 'slide'
    ]) ?>
  <?php if ($block->caption()->isNotEmpty()): ?>
  <figcaption>
    <?= $block->caption()->kirbytext() ?>
  </figcaption>
  <?php endif ?>
</figure>

And this is the blueprint for the page:

title: Voorpagina

icon: 🏠

options:
  changeStatus: false

fields:
  slides:
    label: Slides op de voorpagina
    type: blocks
    required: true
    max: 10
    fieldsets:
      - type: slide
        label: Slide
        preview: fields
        wysiwyg: true
        fields:
          image:
            label: Afbeelding (verplicht)
            type: files
            required: true
            query: site.index.images
            max: 1
            extends: files/image
          caption:
            label: Opschrift (optioneel)
            type: textarea
          link:
            type: link
            required: false
            options:
              - page
              - url

What could be the reason that the isNotEmpty() check is not working the way I expect it to?

Hm, why don’t you handle the block in your block snippet instead of having part of the output in the template and part in the block snippet? Seems a bit weird. But I don’t know why the check is not working as expected. Nevertheless, a first step would be to clean this up and move it to the block snippet.

So having moved the check to the snippet, I have this code for snippets/blocks/slide.php:

    <?php if ($block->link()->isNotEmpty()): ?>
      <a href="<?= $block->link()->toUrl() ?>" class="slide-link">
        <figure>
          <?php $image = $block->image()->toFile();
          snippet('image', 
            [
              'image' => $image,
              'targetUse' => 'slide'
            ]) ?>
          <?php if ($block->caption()->isNotEmpty()): ?>
          <figcaption>
            <?= $block->caption()->kirbytext() ?>
          </figcaption>
          <?php endif ?>
        </figure>
      </a>
    <?php else: ?>
    <figure>
      <?php $image = $block->image()->toFile();
      snippet('image', 
        [
          'image' => $image,
          'targetUse' => 'slide'
        ]) ?>
      <?php if ($block->caption()->isNotEmpty()): ?>
      <figcaption>
        <?= $block->caption()->kirbytext() ?>
      </figcaption>
      <?php endif ?>
    </figure>
    <?php endif; ?>

and templates/home.php:

    <?php snippet('header') ?>
     
    <article class="slider-container swiper">
      <div class="swiper-wrapper">
        <?php foreach($page->slides()->toBlocks() as $slide): ?>
          <div id="<?= $slide->id() ?>" class="swiper-slide slide slide-type-<?= $slide->type() ?>">
            <?= $slide ?>
          </div>
        <?php endforeach; ?>
      </div>
      <!-- pagination -->
      <div class="swiper-pagination"></div>

      <!-- navigation -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>

    </article>

    <?php snippet('footer') ?>

I now get the same result, this is the output (HTML): index.html - Pastebin.com

Looks fine to me, some are wrapped in an a tag, some are not, isn’t that what you wanted? Nevertheless I’d try to avoid the code duplication by just making the a tag conditional, not the whole code snippet

<div id="ac0d5eaa-4e21-432a-a133-4f3bbf3d8e9a" class="swiper-slide slide slide-type-slide">
  <a href="http://localhost:8888/projecten/Werf07" class="slide-link">
    <figure>
      <picture class="image">
     </picture>          
    </figure>
  </a>
</div>

vs

<div id="932f34e1-0257-4f7b-a1ea-db6f5b861230" class="swiper-slide slide slide-type-slide">
  <figure>
    <picture class="image">
    </picture>  
  </figure>
</div>

Yes, apparently it was working just fine. Not sure why I thought it wasn’t. Thank you for checking and for the suggestion to clean up the code.

I’ve re-written the code into this, which I think is cleaner:

<?php 
$image = $block->image()->toFile(); 
$caption = $block->caption();
$link = $block->link()->isNotEmpty() ? $block->link()->toUrl() : null;
?>

<?php if ($link): ?>
  <a href="<?= $link ?>" class="slide-link">
<?php endif; ?>

<figure>
  <?php snippet('image',
   [
    'image' => $image, 
    'targetUse' => 'slide'
   ]); ?>
  <?php if ($caption->isNotEmpty()): ?>
    <figcaption>
      <?= $caption->kirbytext() ?>
    </figcaption>
  <?php endif; ?>
</figure>

<?php if ($link): ?>
  </a>
<?php endif; ?>