@pawel, A rough example might give you an idea to start from.
I presume you have already found the plugin tutorial where you can learn the basic structure of a plugin.
There are many events you can subscribe to and one of them is onTwigSiteVariables
. This is where you should add any variables to Twig that need to be available to Twig during this process.
The following is a sketch of /user/plugins/myplugin/myplugin.php
:
class MyPlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
]);
}
/**
* Add variables to Twig.
*/
public function onTwigSiteVariables()
{
$twig = $this->grav['twig'];
$twig->twig_vars['myClass'] = new MyClass();
$twig->twig_vars['myVariable'] = $myVariable;
}
}
In Twig, you can now call methods and properties of the instance of MyClass or access provided variables.
{% set returnValue = myClass.myMethod() %}
{% set property = myClass.myProperty %}
{% set variable = myVariable %}
Hope this gives you an idea to start from…