I have a layout with a slot for showing tags.
I want to use this layout both when content has tags and when it does not. However, when there are no tags the HTML wrapper for the tags still gets added to the DOM. Thus I want to be able to conditionally show the contents of the slot only when tags are passed.
I’ve tried the $page->tags()->isNotEmpty()
and $page->tags()->exists()
and $page->tags()->isTrue()
but none of these things worked.
I googled for a few hours, couldn’t find any leads, and the docs don’t really expand on what those things do, just reference.
If you need more info, I can provide it.
I’m trying to make sure the snippet doesn’t show the empty list when no tags are passed to the slot. Is this possible?
<?php slot('tags') ?>
<?php snippet('tags') ?>
<?php endslot() ?>
Could you please post your snippet?
<div class="scroll" data-type="inline">
<ul class="tags" data-type="pills">
<?php slot('tag') ?>
<?php endslot() ?>
</ul>
</div>
That is the content of the snippet, the problem is that the scroll and ul wrappers appear in the layout whether I pass any content to the tag slot or not. I want a way to prevent that. Hopefully a straightforward way to check if something was passed or not.
Hi James,
I’m still in the process of learning Kirby and (particularly!) PHP, so this may not work.
However perhaps your issue can be solved by wrapping the snippet in an if statement:
<?php if ($page->tags()->isNotEmpty()): ?>
<div class="scroll" data-type="inline">
<ul class="tags" data-type="pills">
<?php slot('tag') ?>
</ul>
</div>
<?php endif ?>
This way the HTML within your snippet is rendered only if your page content includes tags.
My syntax may be off (or entirely wrong) but hopefully gives you food for thought …and I’m learning loads from my mistakes! ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/apple/slight_smile.png?v=12)
Hi Thank you! I did try that before, and again just now to sanity check, but yeah unfortunately it doesn’t work as I would have expected. I appreciate your time though!