<p>There's a page in the docs you might have missed that covers this here:</p>
<p><a href="https://craftcms.com/docs/5.x/extend/extending-twig.html" rel="nofollow noreferrer">https://craftcms.com/docs/5.x/extend/extending-twig.html</a></p>
<p>So specifically, you want to bind the <code>EVENT_INIT</code> event of the Craft Variable instance:</p>
<p><a href="https://docs.craftcms.com/api/v5/craft-web-twig-variables-craftvariable.html#event-init" rel="nofollow noreferrer">https://docs.craftcms.com/api/v5/craft-web-twig-variables-craftvariable.html#event-init</a></p>

CC BY-SA 4.0
2024-06-04T13:22:01.980Z
2024-06-04T13:22:01.980Z
James Smith
<p>If you started this project with a Craft 5, you have access to Generator, which can scaffold a Twig extension for you! In the project directory, run:</p>
<pre class="lang-bash prettyprint-override"><code>php craft make module

# ...

php craft make twig-extension --module=&lt;your-module-id&gt;
</code></pre>
<p>This will generate the classes and provide all the connective tissue required to expose new Twig filters, functions, and global variables.</p>
<p>Other resources you might be interested in:</p>
<ul>
<li><a href="https://craftcms.com/docs/5.x/extend/behaviors.html" rel="nofollow noreferrer">Behaviors</a> are used to mount functionality to the native <code>craft</code> variable.</li>
<li><a href="https://craftcms.com/docs/5.x/extend/events.html" rel="nofollow noreferrer">Events</a> are typically how you register new components like Twig extensions.</li>
<li>More info on the <a href="https://craftcms.com/docs/5.x/extend/generator.html" rel="nofollow noreferrer">Generator</a> tool, mentioned above.</li>
<li>A conversation about <a href="https://github.com/craftcms/generator/discussions/26" rel="nofollow noreferrer">different ways to expose functionality to Twig</a>.</li>
</ul>
<blockquote>
<p> <strong>Tip:</strong> To generate a <em>behavior</em>, run this:</p>
<pre class="lang-bash prettyprint-override"><code>php craft make behavior
</code></pre>
</blockquote>

CC BY-SA 4.0
2024-06-06T17:51:14.690Z
2024-06-06T17:51:14.690Z
August Miller
<p>I am new to Craft CMS and Yii2 but I'm experienced with PHP a lot.</p>
<p>I'd like to write a custom Twig function (extension) returning a part of a file. For example something like:</p>
<pre><code>{{ craft.myplugin.getFilePart(&quot;myfile.txt&quot;) }}
</code></pre>
<p>There is a documentation for building a module but it's unclear for me:
<a href="https://craftcms.com/docs/5.x/extend/module-guide.html" rel="nofollow noreferrer">https://craftcms.com/docs/5.x/extend/module-guide.html</a></p>
<p>I understand I need to create a PHP file in <code>modules/</code> folder containing PHP class for it. I also understand the PSR-4 concept for autoloading the class in <code>composer.json</code>.</p>
<p><strong>What events should I bind in <code>init()</code> function?</strong> I haven't found any documentation.</p>

<templating><modules><twigextension>
How to create Twig extension in Craft CMS 5
# How to create Twig extension in Craft CMS 5

(Asked by: username_0 on 2024-06-04)

I am new to Craft CMS and Yii2 but I'm experienced with PHP a lot.
I'd like to write a custom Twig function (extension) returning a part of a file. For example something like:
{{ craft.myplugin.getFilePart("myfile.txt") }}

There is a documentation for building a module but it's unclear for me:
https://craftcms.com/docs/5.x/extend/module-guide.html
I understand I need to create a PHP file in modules/ folder containing PHP class for it. I also understand the PSR-4 concept for autoloading the class in composer.json.
What events should I bind in init() function? I haven't found any documentation.

**Tags:** templating, modules, twigextension


---

## 2 answers

### Answer by username_1 on 2024-06-04. Score: 4

There's a page in the docs you might have missed that covers this here:

<https://craftcms.com/docs/5.x/extend/extending-twig.html>

So specifically, you want to bind the `EVENT_INIT` event of the Craft Variable instance:

<https://docs.craftcms.com/api/v5/craft-web-twig-variables-craftvariable.html#event-init>


---

### Answer by username_2 on 2024-06-06. Score: 2

If you started this project with a Craft 5, you have access to Generator, which can scaffold a Twig extension for you! In the project directory, run:
php craft make module

# ...

php craft make twig-extension --module=

This will generate the classes and provide all the connective tissue required to expose new Twig filters, functions, and global variables.
Other resources you might be interested in:

Behaviors are used to mount functionality to the native craft variable.
Events are typically how you register new components like Twig extensions.
More info on the Generator tool, mentioned above.
A conversation about different ways to expose functionality to Twig.


 Tip: To generate a behavior, run this:
php craft make behavior


---

