<p>There are several things going on here. This will be a general answer that can hopefully help you move forward. This example will use <a href="https://github.com/barrelstrength/sprout-active" rel="noreferrer">Sprout Active</a> as a reference and you can see the code here for an example of a plugin that implements multiple, simple Twig Extensions.</p>

<h2>Define your Twig Extension class and Namespace</h2>

<p>In Craft 3, you can register a Twig Extension (or multiple Twig Extensions) in your Plugins <code>init</code>. While the line to register the Twig Extension is straightforward, there are a lot of other details that matter in your file and I'll comment the code below to highlight why:</p>

<pre><code>&lt;?php

// In Craft 3, each class you create has a namespace. You need to 
// make sure that this is referenced in any other classes that use this class
namespace barrelstrength\sproutactive;

// We're referencing the namespaces of other classes in this class too.
// We use the Craft Plugin class, to create our plugin
use craft\base\Plugin;

// You'll create this file next. We need to identify the namespace
// of the Twig Extension we'll be importing so Craft knows where it is
// when we reference `new TwigExtensions()` below
use barrelstrength\sproutactive\twig\TwigExtensions;

class SproutActive extends Plugin
{
    public function init()
    {
        parent::init();

        // Register the Twig Extensions file. We could name this anything
        // and put this anywhere in our folder structure now. As long as 
        // we reference the namespace properly above, Craft will be able to 
        // find it. We can create multiple Twig Extensions within this
        // TwigExtensions Class or create multiple files to manage our
        // Twig Extensions and import those classes too.
        Craft::$app-&gt;view-&gt;twig-&gt;addExtension(new TwigExtensions());
    }
}
</code></pre>

<hr>

<h2>Twig 2 updated several things too</h2>

<p>Once we get the extensions loaded, you can create extensions similar to before, however, Craft 3 uses Twig 2 which introduces several breaking changes on it's own. You can see some discussion of the <a href="https://github.com/craftcms/docs/blob/master/en/changes-in-craft-3.md#twig-2" rel="noreferrer">Twig 2 front-end changes</a> in the Craft docs but the docs don't discuss the API changes.</p>

<p>In Twig 2, <code>Twig_SimpleFunction</code> replaces <code>Twig_Function_Method</code> and <code>Twig_Filter_Method</code> so you'll need to update any Craft 2 Twig Extensions code to use <code>Twig_SimpleFunction</code></p>

<p>In the Sprout Active <code>TwigExtensions</code> Class you can see how the class creates multiple Twig functions within the <code>getFunctions</code> method. </p>

<pre><code>public function getFunctions()
{
    return [
        new \Twig_SimpleFunction('active', [$this, 'getActive']),
        new \Twig_SimpleFunction('activeClass', [$this, 'getActiveClass'], ['is_safe' =&gt; ['html']]),
        new \Twig_SimpleFunction('segment', [$this, 'getSegment']),
        new \Twig_SimpleFunction('segmentClass', [$this, 'getSegmentClass'], ['is_safe' =&gt; ['html']])
    ];
}
</code></pre>

CC BY-SA 4.0
2018-11-29T08:40:23.590Z
So sorry it's taken 11 months to show my appreciation for the Ben! I'm not sure what happened, I'm alway quick to give thanks normally. This is a great and very detailed answer. Thank you very much! :)
Mark Notton
CC BY-SA 3.0
2018-01-01T01:48:45.917Z
2018-01-01T01:48:45.917Z
Ben Parizek
<p>In my plugins init() function I have this: </p>

<pre><code>Craft::$app-&gt;view-&gt;registerTwigExtension(new TestTwigExtension());
</code></pre>

<p>If I duplicate that and change the Twig Extension class name, I just get an error: </p>

<blockquote>
  <p>Class 'marknotton\test\Test2TwigExtension' not found</p>
</blockquote>

<p>In Craft 2.x, this worked: </p>

<pre><code>  public function addTwigExtension() {
    Craft::import('plugins.test.twigextensions.test');
    Craft::import('plugins.test.twigextensions.test2');
    return array(
      new test(),
      new test2()
    );
  }
</code></pre>

<p>It seems that approach has completely changed. I can't rename any file names, Craft::import doesn't exist anymore, and I can't pass an array into the <code>registerTwigExtension()</code> function. (<em>Craft 3 is scary</em>). </p>

<p>What I'm trying to achieve is a way of organising my Twig Extension files, rather than having everything in one file. </p>

CC BY-SA 3.0
2017-12-31T18:41:07.233Z
To be honest I'm a little bit confused what the problem is (I noticed our way of doing this is totally different) But does your `Test2TwigExtension` class exists and did you include it properly? Not sure what you mean with "I can't rename my filenames". When I create 2 separate twig extensions in two different files I can include them both with `Craft::$app->view->registerTwigExtension(new ClassName());`
Robin Schambach
<plugin-development><craft3><twigextension>
Register multiple TwigExtensions in one plugin in Craft 3
# Register multiple TwigExtensions in one plugin in Craft 3

(Asked by: username_0 on 2017-12-31)

In my plugins init() function I have this: 
Craft::$app->view->registerTwigExtension(new TestTwigExtension());

If I duplicate that and change the Twig Extension class name, I just get an error: 

Class 'marknotton\test\Test2TwigExtension' not found

In Craft 2.x, this worked: 
  public function addTwigExtension() {
    Craft::import('plugins.test.twigextensions.test');
    Craft::import('plugins.test.twigextensions.test2');
    return array(
      new test(),
      new test2()
    );
  }

It seems that approach has completely changed. I can't rename any file names, Craft::import doesn't exist anymore, and I can't pass an array into the registerTwigExtension() function. (Craft 3 is scary). 
What I'm trying to achieve is a way of organising my Twig Extension files, rather than having everything in one file.

**Tags:** plugin-development, craft3, twigextension

> **Comments:**
> **username_1**: To be honest I'm a little bit confused what the problem is (I noticed our way of doing this is totally different) But does your `Test2TwigExtension` class exists and did you include it properly? Not sure what you mean with "I can't rename my filenames". When I create 2 separate twig extensions in two different files I can include them both with `Craft::$app->view->registerTwigExtension(new ClassName());`  

---

## 1 answers

### Answer by username_2 on 2018-01-01. Score: 7 | Accepted answer

There are several things going on here. This will be a general answer that can hopefully help you move forward. This example will use Sprout Active as a reference and you can see the code here for an example of a plugin that implements multiple, simple Twig Extensions.
Define your Twig Extension class and Namespace
In Craft 3, you can register a Twig Extension (or multiple Twig Extensions) in your Plugins init. While the line to register the Twig Extension is straightforward, there are a lot of other details that matter in your file and I'll comment the code below to highlight why:
view->twig->addExtension(new TwigExtensions());
    }
}


Twig 2 updated several things too
Once we get the extensions loaded, you can create extensions similar to before, however, Craft 3 uses Twig 2 which introduces several breaking changes on it's own. You can see some discussion of the Twig 2 front-end changes in the Craft docs but the docs don't discuss the API changes.
In Twig 2, Twig_SimpleFunction replaces Twig_Function_Method and Twig_Filter_Method so you'll need to update any Craft 2 Twig Extensions code to use Twig_SimpleFunction
In the Sprout Active TwigExtensions Class you can see how the class creates multiple Twig functions within the getFunctions method. 
public function getFunctions()
{
    return [
        new \Twig_SimpleFunction('active', [$this, 'getActive']),
        new \Twig_SimpleFunction('activeClass', [$this, 'getActiveClass'], ['is_safe' => ['html']]),
        new \Twig_SimpleFunction('segment', [$this, 'getSegment']),
        new \Twig_SimpleFunction('segmentClass', [$this, 'getSegmentClass'], ['is_safe' => ['html']])
    ];
}

> **Comments:**
> **username_0**: So sorry it's taken 11 months to show my appreciation for the Ben! I'm not sure what happened, I'm alway quick to give thanks normally. This is a great and very detailed answer. Thank you very much! :)  

---

