Answers.Body: <p>There are two ways to achieve this:</p>
  
  <p><strong>Option 1</strong></p>
  
  <p>Add this in your plugin/module main init function:</p>
  
  <pre><code>      $twig = Craft::$app-&gt;view-&gt;getTwig(null, ['safe_mode' =&gt; false]);
        $twig-&gt;addGlobal('foo', 'bar');
  </code></pre>
  
  <p>That's it. You can call 'foo' in your template like this <code>{{ foo }} // Output 'bar'</code></p>
  
  <p><strong>Option 2 (best)</strong></p>
  
  <p>In the root of your plugin/module directory, create a folder called 'twigextensions' if you don't already have one. Create a new file where you're going to define your globals. <code>MyGlobals.php</code> for example.</p>
  
  <p>At the very top of your main plugin/module file, you need to add this:</p>
  
  <pre><code>use modules\myawesomemodile\twigextensions\MyGlobals;
  </code></pre>
  
  <p>... remembering to amend the plugin/module name, and the name of the file you created. </p>
  
  <p>Then add the following to the same file. Only this needs to be inside your main init function: </p>
  
  <pre><code>Craft::$app-&gt;view-&gt;registerTwigExtension(new MyGlobals());
  </code></pre>
  
  <p>Lastly, in your new MyGlobals.php file. Add the following. Again changning the plugin/module names where appropriate. </p>
  
  <pre><code>namespace modules\myawesomemodule\twigextensions;
  
  use modules\myawesomemodule\MyAwesomeModule;
  
  use Craft;
  
  class MyGlobals extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
  {
  
      public function getGlobals()
      {
          return array(
              'foo' =&gt; 'bar',
          );
      }
  }
  </code></pre>
  
  <p>That's it. Like before, you can call 'foo' in your template like this <code>{{ foo }} // Output 'bar'</code></p>
  
  <p>Now I don't understand the technical reasons as to why one is better than the other. I prefer <strong>Option 2</strong> because it permits me to add multiple globals. However, my old post was answered by <a href="https://craftcms.stackexchange.com/users/1098/mats-mikkel-rummelhoff">Mats Mikkel Rummelhoff</a>; who goes into this in a <a href="https://craftcms.stackexchange.com/questions/16491/does-twigs-addglobal-exist-in-craft#answer-16492">little more detail</a>. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-13T23:29:07.050Z
Answers.LastActivityDate: 2018-02-13T23:29:07.050Z
Answers.UserDisplayName: Mark Notton
Body: <p>A long time ago <a href="https://craftcms.stackexchange.com/questions/16491/does-twigs-addglobal-exist-in-craft">I asked how I could utilise the getGlobal method in Craft 2.x</a>. </p>
  
  <p>I'd like to know how best to do this in Craft 3?</p>
  
Tags: <plugin-development><craft3><globals>
Title: Defining TwigExtension globals in Craft 3
ThreadText: # Defining TwigExtension globals in Craft 3
  
  (Asked by: username_0 on 2018-02-13)
  
  A long time ago [I asked how I could utilise the getGlobal method in Craft 2.x](https://craftcms.stackexchange.com/questions/16491/does-twigs-addglobal-exist-in-craft).
  
  I'd like to know how best to do this in Craft 3?
  
  **Tags:** plugin-development, craft3, globals
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-02-13. Score: 2 | Accepted answer
  
  There are two ways to achieve this:
  
  **Option 1**
  
  Add this in your plugin/module main init function:
  
  ```
        $twig = Craft::$app->view->getTwig(null, ['safe_mode' => false]);
        $twig->addGlobal('foo', 'bar');
  
  ```
  
  That's it. You can call 'foo' in your template like this `{{ foo }} // Output 'bar'`
  
  **Option 2 (best)**
  
  In the root of your plugin/module directory, create a folder called 'twigextensions' if you don't already have one. Create a new file where you're going to define your globals. `MyGlobals.php` for example.
  
  At the very top of your main plugin/module file, you need to add this:
  
  ```
  use modules\myawesomemodile\twigextensions\MyGlobals;
  
  ```
  
  ... remembering to amend the plugin/module name, and the name of the file you created.
  
  Then add the following to the same file. Only this needs to be inside your main init function:
  
  ```
  Craft::$app->view->registerTwigExtension(new MyGlobals());
  
  ```
  
  Lastly, in your new MyGlobals.php file. Add the following. Again changning the plugin/module names where appropriate.
  
  ```
  namespace modules\myawesomemodule\twigextensions;
  
  use modules\myawesomemodule\MyAwesomeModule;
  
  use Craft;
  
  class MyGlobals extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
  {
  
      public function getGlobals()
      {
          return array(
              'foo' => 'bar',
          );
      }
  }
  
  ```
  
  That's it. Like before, you can call 'foo' in your template like this `{{ foo }} // Output 'bar'`
  
  Now I don't understand the technical reasons as to why one is better than the other. I prefer **Option 2** because it permits me to add multiple globals. However, my old post was answered by [Mats Mikkel Rummelhoff](https://craftcms.stackexchange.com/users/1098/mats-mikkel-rummelhoff); who goes into this in a [little more detail](https://craftcms.stackexchange.com/questions/16491/does-twigs-addglobal-exist-in-craft#answer-16492).
  
  
  ---
  
  