<p>Turns out I was missing the crucial part of exposing the class to twig itself. I didn't know I had to do that.</p>
<p>So the solution ended up being to add this to the plugin init:</p>
<pre class="lang-php prettyprint-override"><code>Event::on(
    CraftVariable::class,
    CraftVariable::EVENT_INIT,
    function (Event $event) {
        $variable = $event-&gt;sender;
        $variable-&gt;set('ginacpmods', GinaCp::class);
    }
);
</code></pre>

CC BY-SA 4.0
2024-10-07T08:48:05.017Z
2024-10-07T08:48:05.017Z
InanisAtheos
<p>I have created a (private) plugin, <code>ginacpmods</code>. The main class is called <code>GinaCp</code> which extends <code>Plugin</code>.</p>
<p>The plugin has an admin page which is rendered with <code>templates/index.twig</code>. This twig file contains a button which, will execute <code>craft.ginacpmods.clearAll()</code>. This doesn't work. All I get back is</p>
<p><code>Calling unknown method: craft\web\twig\variables\CraftVariable::ginacpmods()</code></p>
<p>I've tried referencing <code>ginacp</code>, <code>ginacpmods</code>, <code>_ginacpmods</code> but neither works. I'm starting to wonder what I'm missing.</p>
<p>The function I'm calling is dead simple right now and doesn't really do anything yet:</p>
<h5>plugins/ginacpmods/src/GinaCp.php:</h5>
<pre class="lang-php prettyprint-override"><code>class GinaCp extends Plugin {
    [...]

    public function clearAll(): bool|array|string {
        return 'Done.';
    }
</code></pre>
<p>Is there something I need to do in order to expose the clearAll() function to twig?</p>

<plugin-development><templating>
Execute function within my plugin, through a twig (Admin page) template
# Execute function within my plugin, through a twig (Admin page) template

(Asked by: username_0 on 2024-10-07)

I have created a (private) plugin, ginacpmods. The main class is called GinaCp which extends Plugin.
The plugin has an admin page which is rendered with templates/index.twig. This twig file contains a button which, will execute craft.ginacpmods.clearAll(). This doesn't work. All I get back is
Calling unknown method: craft\web\twig\variables\CraftVariable::ginacpmods()
I've tried referencing ginacp, ginacpmods, _ginacpmods but neither works. I'm starting to wonder what I'm missing.
The function I'm calling is dead simple right now and doesn't really do anything yet:
plugins/ginacpmods/src/GinaCp.php:
class GinaCp extends Plugin {
    [...]

    public function clearAll(): bool|array|string {
        return 'Done.';
    }

Is there something I need to do in order to expose the clearAll() function to twig?

**Tags:** plugin-development, templating

---

## 1 answers

### Answer by username_0 on 2024-10-07. Score: 1

Turns out I was missing the crucial part of exposing the class to twig itself. I didn't know I had to do that.
So the solution ended up being to add this to the plugin init:
Event::on(
    CraftVariable::class,
    CraftVariable::EVENT_INIT,
    function (Event $event) {
        $variable = $event->sender;
        $variable->set('ginacpmods', GinaCp::class);
    }
);


---

