Plugin controller for block plugin

Hi community,
I am working on a block plugin and would like to handle the logic in a controller instead inside the snippet. My plugin structure looks like this:

myplugin
– blueprints
---- blocks
------ myplugin.yml
– controllers
---- blocks
------ myplugin.php
– snippets
---- blocks
------ myplugin.php
– composer.json
– index.js
– index.php

index.php:

<?php

Kirby::plugin('me/myplugin', [
  'blueprints' => [
    'blocks/myplugin' => __DIR__ . '/blueprints/blocks/myplugin.yml',
  ],
  'snippets' => [
    'blocks/myplugin' => __DIR__ . '/snippets/blocks/myplugin.php',
  ],
  'controllers' => [
    'blocks/myplugin' => require __DIR__ . '/controllers/blocks/myplugin.php',
]
]);

controllers/blocks/myplugin.php:

<?php
return function ($site) {
    return [
        'myVar' => $site->title();
    ];
}; 

snippets/blocks/myplugin.php:

<h1><?= $myVar ?></h1>

The usage of $myVar in snippets/blocks/myplugin.php throws an error saying: β€œUndefined variable for myVar”.

Any idea on how to use the controller properly?

There is no such thing as a block controller. Where did you get that from?

You can try block models:

use Kirby\Cms\Block;

class HeadingBlock extends Block
{
    public function customMethod(): string
    {
        return '<h1>Custom HTML for headings</h1>';
    }
    
    public function controller(): array
	{
		return [
			'myVar'   => site()->title(),
            
			'block'   => $this,
			'content' => $this->content(),
			// deprecated block data
			'data'    => $this,
			'id'      => $this->id(),
			'prev'    => $this->prev(),
			'next'    => $this->next()
		];
	}
}

Kirby::plugin('my/blockModels', [
    'blockModels' => [
        'heading' => HeadingBlock::class
    ]
]);

You can override block controller method:

I don’t remember where i saw it first or if i came up with it. :sweat_smile:
Wouldn’t it be logical to have sth like a plugin controller to keep the logic out of the plugin snippets?