<p>Plugins don't provide any variables or functions to Twig by default, you need to do that explicitly. Check out the <a href="https://craftcms.com/docs/4.x/extend/extending-twig.html" rel="nofollow noreferrer">Extending Twig guide</a>. You can find two good options there.</p>
<p>I would recommend using a Twig extension instead of extending the global <code>craft</code> variable. This is better in terms of separation of concerns, and easier to understand for new developers working on a project.</p>

CC BY-SA 4.0
2023-10-20T17:57:56.743Z
Thanks for replying. I'm going to pursue building a module instead which seems a bit simpler for learning purposes. [Business Logic](https://github.com/doublesecretagency/craft-businesslogic) is a good tool with supporting documentation. I believe Generator also has an option to create a fully commented framework.
Jason McKinney
CC BY-SA 4.0
2023-10-23T08:05:04.057Z
@JasonMcKinney Starting with a module is definitely a good idea!
MoritzLost
CC BY-SA 4.0
2023-10-20T07:55:56.020Z
2023-10-20T07:55:56.020Z
MoritzLost
<p>Writing my first plugin. Scaffolded with Generator and is active (appearing in admin). This is where most tutorials leave you on your own so hoping to get some help calling a simple function in my templates.</p>
<p>In pluginname/src/Plugin.php I have the following function:</p>
<pre><code>class Plugin extends BasePlugin
{
    public function hello() {
       return &quot;Hello World&quot;;
    }
}
</code></pre>
<p>Template:</p>
<pre><code>{{ pluginname.hello }}
</code></pre>
<p>Craft is not recognizing 'pluginname' as a variable so the function will not run. I'm obviously missing a critical concept here and hoping someone can point me in the right direction. Thanks.</p>

<plugin-development><craft4>
Calling a Function From Private Plugin
# Calling a Function From Private Plugin

(Asked by: username_0 on 2023-10-19)

Writing my first plugin. Scaffolded with Generator and is active (appearing in admin). This is where most tutorials leave you on your own so hoping to get some help calling a simple function in my templates.
In pluginname/src/Plugin.php I have the following function:
class Plugin extends BasePlugin
{
    public function hello() {
       return "Hello World";
    }
}

Template:
{{ pluginname.hello }}

Craft is not recognizing 'pluginname' as a variable so the function will not run. I'm obviously missing a critical concept here and hoping someone can point me in the right direction. Thanks.

**Tags:** plugin-development, craft4


---

## 1 answers

### Answer by username_1 on 2023-10-20. Score: 2 | Accepted answer

Plugins don't provide any variables or functions to Twig by default, you need to do that explicitly. Check out the [Extending Twig guide](https://craftcms.com/docs/4.x/extend/extending-twig.html). You can find two good options there.

I would recommend using a Twig extension instead of extending the global `craft` variable. This is better in terms of separation of concerns, and easier to understand for new developers working on a project.

> **Comments:**
> **username_0**: Thanks for replying. I'm going to pursue building a module instead which seems a bit simpler for learning purposes. [Business Logic](https://github.com/doublesecretagency/craft-businesslogic) is a good tool with supporting documentation. I believe Generator also has an option to create a fully commented framework.  
> **username_1**: @JasonMcKinney Starting with a module is definitely a good idea!  

---

