Negated isEmpty and isNotEmpty seem to work differently

Hey there, I’ve been updating one of my themes’s conditional statements and I ran across a possible bug.

The original statement is this:

<? if (!$contributors->isEmpty()): ?>
	<h2><?= l::get('contributors') ?></h2>
<? endif; ?>

This works. Only when $contributors has a value, the <h2> is displayed.

When I updated it to this:

<? if ($contributors->isNotEmpty()): ?>
	<h2><?= l::get('contributors') ?></h2>
<? endif; ?>

It doesn’t work as expected. The <h2> is desplayed regardless of the content of $contributors.

Any ideas?

Could it be that you still have an old version, which does not have the isNotEmpty() method at all?

The panel says:

Toolkit version: 2.1.0
Kirby version: 2.1.0
Panel version: 2.1.0

Note that $contributors is a structure field. Does it matter?

$contributors = $page->contributors()->toStructure();

How does does isNotEmpty() interact with structure fields?

I have debug on, so if there was an unexpected problem or exception, I should get an error message on the page.



<?php $contributors = $page->contributors(); ?>

and both statements (the ifs) run!




to something like

<?php foreach($contributors->toStructure() as $contributor): ?>
      <p><?php echo $contributor->name()->html(); ?><br />      
            <?php echo $contributor->street()->html(); ?><br />
            <?php echo $contributor->zip()->html(); ?>&nbsp;<?php echo $contributor->city()->html(); ?></p>
<?php endforeach ?>

Of course you can change “<p>” to “<ul>” (outside the foreach statement) and “<li>” and so on as you like.

Good luck!

1 Like

Worked perfectly! Thanks.

Hello, I’m having the same problem, though I’m not using structure fields – just the straightforward method mentioned in the docs:

<div class = "metadata cf">
      <?php if($page->format()->isNotEmpty()): ?><div><p>Format:<br><?php echo $page->format()->html() ?></p></div><?php endif ?>
      <?php if($page->sites()->isNotEmpty()): ?><div><p>Site:<br><?php echo $page->sites()->html() ?></p></div><?php endif ?>
      <?php if($page->collaborator()->isNotEmpty()): ?><div><p>Collaborator:<br><?php echo $page->collaborator()->html() ?></p></div><?php endif ?>

Like Paul, the “isEmpty” statement works when tested, but “isNotEmpty” will display regardless of content. Wondering if I’m missing something in this specific code, as I’ve used this method before and it worked perfectly…

I can’t reproduce this issue. Which Kirby version do you use?

Please tell us for which line(s) you find:

Ah, it was because I had an older version of Kirby – I reinstalled everything and it works fine now. Thank you for your help!

You are welcome. Glad that it works now.