Mark_E
March 21, 2025, 3:00pm
1
I have an event.php block. I’d like to add a snippet to the block. Is this possible?
Here’s a simple version of my block
<div class="event">
<?= $block->event_description()->kirbytext() ?>
<?php snippet('button-snippet') ?>
</div>
And my template uses:
<?= $page->events()->toBlocks() ?>
Please describe the problem you are facing.
Mark_E
March 21, 2025, 3:12pm
3
Hi. Is it possible to have a snippet inside a block? If so, how is it done? I just get a PHP error “Block error: “Undefined variable $block” in block type: “event”” and the blocks are not displaying.
It works fine without <?php snippet('button-snippet') ?>
but when I add this the blocks stop displaying.
Thanks
Then you probably use a variable in your snippet, without having passed it to the snippet from the block snippet calling it, see Snippets | Kirby CMS .
Mark_E
March 21, 2025, 5:43pm
5
I appear to have got it working
<div class="book-button">
<?php $buttonText = $block->button_text()->or('Book')->esc(); ?>
<?php if ($block->button_link()->isNotEmpty()): ?>
<a href="<?= $block->button_link()->esc() ?>" target="_blank"><?= $buttonText ?></a>
<?php elseif ($block->button_email()->isNotEmpty()): ?>
<a href="mailto:<?= $block->button_email()->esc() ?>"><?= $buttonText ?></a>
<?php else: ?>
<a href="mailto:"><?= $buttonText ?></a>
<?php endif; ?>
</div>
And the block code linking to the snippet
<?php snippet('button-block-snippet', ['block' => $block]) ?>