So, I’ve created a custom FAQ block as described in Block factory: Creating your own blocks collection | Kirby CMS
Now, I would assume in the page template I’d only have to do <?= $page->fieldname()->toBlocks() ?>
and it would print the HTML that is stored in the block snippets templates but on my page it’s not printing anything. If I do <?php dump($page->fieldname()) ?>
it outputs a field object with all the content and if I do <?php dump($page->fieldname()->toBlocks()) ?>
it outputs a blocks object with the appropriate amount of items. Why isn’t it printing the snippet code?
Here’s the relevant code:
// ~/site/plugins/faq/index.php
Kirby::plugin('example/faq-block', [
'blueprints' => [
'blocks/accordion' => __DIR__ . '/blueprints/blocks/accordion.yml',
'blocks/faq' => __DIR__ . '/blueprints/blocks/faq.yml'
],
'snippets' => [
'blocks/accordion' => __DIR__ . '/snippets/blocks/accordion.php',
'blocks/faq' => __DIR__ . '/snippets/blocks/faq.php'
]
]);
# ~/site/plugins/faq/blueprints/blocks/accordion.yml
name: "Accordion"
icon: bars
fields:
summary:
label: Summary
type: writer
placeholder: Enter question …
details:
label: Detail
type: writer
marks: true
<!-- ~/site/plugins/faq/snippets/blocks/accordion.php -->
<?php if($block->summary()->isNotEmpty()): ?>
<details class="accordion-details">
<summary class="accordion-summary"><?= $block->summary() ?></summary>
<div class="accordion-text"><?= $block->details() ?></div>
</details>
<?php endif; ?>
# ~/site/plugins/faq/blueprints/blocks/faq.yml
name: "FAQ List"
icon: question
fields:
heading:
label: "Section heading"
type: writer
inline: true
marks: false
faq:
label: FAQ
type: blocks
fieldsets:
- accordion
<!-- ~/site/plugins/faq/snippets/blocks/faq.php -->
<?php $faqItems = $block->faq()->toBlocks(); ?>
<div class="faq-section">
<?php if($faqItems->isNotEmpty()): ?>
<h2><?= $block->heading() ?></h2>
<?= $faqItems ?>
<?php endif; ?>
</div>
And my actual page blueprint and template (reduced):
title: FAQ
fields:
content:
type: blocks
fieldsets:
- faq
<main>
<article>
<h2><?= $page->title() ?></h2>
<?= $page->content()->toBlocks() ?> <!-- nothing is printed here -->
</article>
</main>
…
I can manually loop over the blocks object and add the relevant HTML but there should be some default output from the plugin, shouldn’t there?