Answers.Body: <p>You'll need to register your variable in your plugin's <code>init()</code> method, like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>Event::on(
      CraftVariable::class,
      CraftVariable::EVENT_INIT,
      function (Event $event) {
          /** @var CraftVariable $variable */
          $variable = $event-&gt;sender;
          $variable-&gt;set('pluginname', PluginVariable::class);
      }
  );
  </code></pre>
  
  <p>c.f.: <a href="https://docs.craftcms.com/v3/extend/extending-twig.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/extend/extending-twig.html</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-13T23:31:04.653Z
Answers.Text: Ok did that, looks like there is more to it than this... viewed another plugin I installed from app store. Created a variables directory with the variables file. Added use pluginroot/pluginname/variables/pluginvariable How do you call it using twig from a template file? Seems to be way more involved than craft2 apparently.
Answers.UserDisplayName: Bowenac
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-14T00:00:14.540Z
Answers.Text: I might have just figured it out, will update later if I did, sorting out other deprecated errors.
Answers.UserDisplayName: Bowenac
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-14T00:16:12.223Z
Answers.Text: Got it figured out, will update original post with more info on how I got it to work... for anyone else with same issue. Not a lot detailed docs on this stuff...
Answers.UserDisplayName: Bowenac
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-15T02:49:00.923Z
Answers.Text: You can generate your plugin scaffolding at https://pluginfactory.io and it'll do all of this for you
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-13T22:17:16.790Z
Answers.LastActivityDate: 2018-12-13T23:15:49.360Z
Answers.LastEditDate: 2018-12-13T23:15:49.360Z
Answers.UserDisplayName: andrew.welch
Body: <p>In craft 2 I had a simple plugin that I used to run some php code. I could call the function like this.</p>
  
  <pre><code>{{ craft.pluginname.functionname }}
  </code></pre>
  
  <p>However I have been unable to do the same thing in craft 3. I have created a new plugin for craft 3. Moved my functions to the main plugin file in src.</p>
  
  <p>Is there a new way to call a function from a plugin in craft 3?</p>
  
  <p><strong>Ok got it figured out.</strong></p>
  
  <p>author below = root plugin path in vendor folder</p>
  
  <p>In main plugin file had to add</p>
  
  <pre><code>use author\pluginname\variables\variablename;
  use craft\web\twig\variables\CraftVariable;
  </code></pre>
  
  <p>Updated init event to</p>
  
  <pre><code>Event::on(
      CraftVariable::class,
      CraftVariable::EVENT_INIT,
      function (Event $event) {
          /** @var CraftVariable $variable */
          $variable = $event-&gt;sender;
          $variable-&gt;set('pluginname', variablename::class);
      }
  );
  </code></pre>
  
  <p>Created variables file in variables folder src/variables/variablename.php</p>
  
  <pre><code>namespace author\pluginname\variables;
  use author\pluginname\mainClassName;
  use Craft;
  
  class variablename
  {
      //Custom Functions Here
      public function test() {
          echo 'Test';
      }
  }
  </code></pre>
  
  <p>Then from a template I was able to use</p>
  
  <pre><code>{{ craft.pluginname.test }}
  </code></pre>
  
Tags: <plugin-development><craft3>
Title: Call function from plugin in template
ThreadText: # Call function from plugin in template
  
  (Asked by: username_0 on 2018-12-13)
  
  In craft 2 I had a simple plugin that I used to run some php code. I could call the function like this.
  
  ```
  {{ craft.pluginname.functionname }}
  
  ```
  
  However I have been unable to do the same thing in craft 3. I have created a new plugin for craft 3. Moved my functions to the main plugin file in src.
  
  Is there a new way to call a function from a plugin in craft 3?
  
  **Ok got it figured out.**
  
  author below = root plugin path in vendor folder
  
  In main plugin file had to add
  
  ```
  use author\pluginname\variables\variablename;
  use craft\web\twig\variables\CraftVariable;
  
  ```
  
  Updated init event to
  
  ```
  Event::on(
      CraftVariable::class,
      CraftVariable::EVENT_INIT,
      function (Event $event) {
          /** @var CraftVariable $variable */
          $variable = $event->sender;
          $variable->set('pluginname', variablename::class);
      }
  );
  
  ```
  
  Created variables file in variables folder src/variables/variablename.php
  
  ```
  namespace author\pluginname\variables;
  use author\pluginname\mainClassName;
  use Craft;
  
  class variablename
  {
      //Custom Functions Here
      public function test() {
          echo 'Test';
      }
  }
  
  ```
  
  Then from a template I was able to use
  
  ```
  {{ craft.pluginname.test }}
  
  ```
  
  **Tags:** plugin-development, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-13. Score: 3 | Accepted answer
  
  You'll need to register your variable in your plugin's `init()` method, like this:
  
  ```php
  Event::on(
      CraftVariable::class,
      CraftVariable::EVENT_INIT,
      function (Event $event) {
          /** @var CraftVariable $variable */
          $variable = $event->sender;
          $variable->set('pluginname', PluginVariable::class);
      }
  );
  
  ```
  
  c.f.: <https://docs.craftcms.com/v3/extend/extending-twig.html>
  
  > **Comments:**
  > **username_0**: Ok did that, looks like there is more to it than this... viewed another plugin I installed from app store. Created a variables directory with the variables file. Added use pluginroot/pluginname/variables/pluginvariable How do you call it using twig from a template file? Seems to be way more involved than craft2 apparently.  
  > **username_0**: I might have just figured it out, will update later if I did, sorting out other deprecated errors.  
  > **username_0**: Got it figured out, will update original post with more info on how I got it to work... for anyone else with same issue. Not a lot detailed docs on this stuff...  
  > **username_1**: You can generate your plugin scaffolding at https://pluginfactory.io and it'll do all of this for you  
  
  ---
  
  