How to create a Kirby Collection similar to those from the Builder plugin?

I am using the approach suggested by the Kirby Builder plugin’s docs: small snippets (one for each block type), and within those I make use of Kirby’s chaining syntax to work with the data.

Now, I have a few instances in my templates where I would like to call the same snippets directly (i.e. without involvement of the builder). Use case: While users can add a “Donation banner” block using the builder, I also want to hardcode them into a few pages’ templates by default. This breaks as soon as the snippets contain the processing of variables, e.g. <?= $data->calltoaction() ?>.

This shouldn’t be too difficult, but I am terribly stuck with how to achieve this. I would like to be able to call a custom page method (e.g. builderblock()) as follows and it would render the block snippet donate, dealing with the data as if it were handed over by the plugin’s toBuilderBlocks() method, i.e. enabling all the same chaining operations etc.

    '_key' => 'donate',
    'content' => [
        'calltoaction' => 'Donate now',
        'img' => 'donation.svg'

How do I get this content handed over to the snippet in the same format as the Builder plugin does? Any hints in the right direction would be greatly appreciated!

$data is a StructureObject, you can create an instance of it like this:

$data = new StructureObject([
    'id' => '1',
    'content' => [
        'calltoaction' => 'Donate now',
        'img'          => 'donation.svg'

Thank you, that was the piece I was missing - turns out I was close, but had been falsely using new Collection(). Now it works and I can use the same blocks both with an without the builder!

If I may ask a bonus question, since I could not figure it out from docs/source: does it matter what value I pass as id? It is required (causes error when ommited) but I wonder how to avoid reusing an ID already in use?

The id can be any string, you would have to make sure that your ids are unique if you have a collection of StructureObject objects (i.e. Structure object), but for a single item, I think it doesn’t really matter.

1 Like