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.
Change
to
<?php $contributors = $page->contributors(); ?>
and both statements (the if
s) run!
###Hint:
Change
[...]
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(); ?> <?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.
em_elle
September 29, 2016, 9:53pm
6
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 ?>
</div>
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…
texnixe
September 29, 2016, 10:56pm
7
I can’t reproduce this issue. Which Kirby version do you use?
Please tell us for which line(s) you find:
em_elle
September 30, 2016, 5:09pm
9
Ah, it was because I had an older version of Kirby – I reinstalled everything and it works fine now. Thank you for your help!
texnixe
September 30, 2016, 5:16pm
10
You are welcome. Glad that it works now.