Nested blocks
This is an example of how to build a nested block including a live panel preview and output snippets for the website. In our example we are creating a FAQ block, which allows us to add nested question/answer blocks within it. The block is built as a standalone plugin and can be extended and modified to your own needs. The example is based on a blank Kirby installation (v.3.5+) and everything is explained step by step.
Preparation
First we prepare our Kirby config file to enable custom blocks. We keep the Kirby default blocks grouped, and separate our new custom blocks for a better overview.
The blocks field can now be used without adding the new FAQ block type manually:
Blueprints
Now let's create the blueprint for the FAQ block itself, and the blueprint for the nested block with a question and answer field.
The name of the block (faq
in our example) must be unique and must not already be used by Kirby (e.g. heading
, text
, etc.). If the name is already in use, you can simply add a prefix or suffix to it (e.g. my_custom_faq
).
FAQ blueprint
The first blueprint defines our FAQ block. Besides our new nested block we use an additional text
field and a writer
field in our example.
Question/answer blueprint (nested)
The second blueprint defines the nested block with the question
and the answer
fields. Hereafter we call this block faqItem
.
Output
Now we build the output snippets for the website. We need a snippet for the faq
block and a snippet for the faqItem
block.
Default template
For completeness, we also show the default
template here, which can be used to render our newly created faq
block.
FAQ snippet
The first output snippet is for the FAQ block. We show headline
and text
of this block as usual and we call the nested block(s) as follows.
Question/answer snippet
The second snippet defines the output of the nested question/answer block.
The plugin
Now let's create the plugin itself. The plugin needs only two files to work: index.php
and index.js
. An optional file index.css
allows individual stylesheets for the preview.
index.php
In the index.php
file we register our blueprints and output snippets, respectively for the FAQ block (faq
) and the question/answer block (faqItem
).
index.js
The index.js
file handles the previews inside the Panel. We define a template for faq
and faqItem
.
index.css
The .css
file is optional and can be used to style the preview for the block (and the nested blocks if needed).
Summary
That's it! Now you have a fully functional example of how nested blocks are working. Add your own fields and styles to your block as you like.
The following files are used in our example:
blueprints
blocks
- faq.yml
- faqItem.yml
snippets
blocks
- faq.php
- faqItem.php
- index.css
- index.js
- index.php
Feel free to download all needed files here: Nested blocks example.
Advanced usage
In some cases it is necessary to pass collected values from the parent block to the nested block, if you need different (nested) HTML markup depending on which option is selected. This is actually pretty simple if you know how. The next step explains how to extend nested blocks with additional layouts.
Blueprints
First let's change the blueprints with the new requirements. We add a select field layout
to our FAQ block and offer two layout variants for it.
Now we modify the output snippets with our new requirements.
FAQ snippet
We can deliver different HTML markup depending on which version we are in by checking the layout
value.
Now we call the nested block within each version as follows. It is important to pass the selected layout
to the nested block, if we want to use different HTML markup for each version.
Question/answer snippet
By passing the layout value from the parent block, we can use it via $data['layout']
in the nested block now. We just need to check the value and can deliver different HTML output for each version.
Conclusion
Now you can create your own layouts regardless of the HTML markup you need. As a content creator you have the full flexibility to duplicate complete blocks, change the layout and compare which version fits better to your needs.