Answers.Body: <p>Not 100% sure what is going wrong here, but here is what I would do to debug this on my end:</p>
  
  <ol>
  <li><p>Simply output the current path the template parser is using in the controller and event handler to see the difference in the values</p>
  
  <pre><code>var_dump(craft()-&gt;path-&gt;getTemplatesPath());
  exit();
  </code></pre></li>
  <li><p>If the values are different, you can override the value to do your parsing as your need it like so:</p>
  
  <pre><code>// Get the current path and save it
  $oldPath = craft()-&gt;path-&gt;getTemplatesPath();
  
  // Set your desired template path
  craft()-&gt;path-&gt;setTemplatesPath('your/new/path');
  
  // Render your template and pass some data to it that will be parsed
  craft()-&gt;templates-&gt;render('your/template', $data = array());
  
  // Set the old path so Craft can resume as normal and you don't break stuff
  craft()-&gt;path-&gt;setTemplatesPath($oldPath);
  </code></pre></li>
  <li><p>One other option is to try to include your macro like so:</p>
  
  <pre><code>{% import 'myplugin/_macros/mymacro' as mymacro % }
  </code></pre></li>
  </ol>
  
  <p>I basically do the exact same thing you are doing with <a href="https://github.com/objectivehtml/Postmaster-for-Craft-CMS" rel="nofollow">Postmaster for Craft</a>. I include my own macros, parse templates and strings during event handlers, it all works perfectly for me using the technique I outlined above. Feel free to take a look at my code if you want to see some more advanced examples of this.</p>
  
  <p>On a side note, I bet Postmaster will do exactly what you want to do in regards to sending an email for new entries. You should give it a shot, it has loads of additional features I bet you could use. No less, I do help this solve your problem either way. Sorry this wasn't really an official answer or a guaranteed fix, but I gave it my best shot without seeing all of your code.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-16T19:52:39.607Z
Answers.LastActivityDate: 2015-02-16T20:00:05.437Z
Answers.LastEditDate: 2015-02-16T20:00:05.437Z
Answers.UserDisplayName: Justin Kimbrell
Body: <p>I have a listener for the <code>entries.saveEntry</code>event that is calling a service, which generates an email from a rendered template. The template is attempting to import macros.</p>
  
  <p>MyaddonPlugin.php::init()</p>
  
  <pre><code>craft()-&gt;on('entries.saveEntry', function(Event $event) {
      if ($event-&gt;params['isNewEntry']) {
          craft()-&gt;myaddon_emails-&gt;generateEmail($event-&gt;params['entry']);
      }
  });
  </code></pre>
  
  <p>Myaddon_EmailsService.php::generateEmail()</p>
  
  <pre><code>$emailHtml = craft()-&gt;templates-&gt;render('myaddon/email');
  </code></pre>
  
  <p>plugins/myaddon/templates/email.twig</p>
  
  <pre><code>{% import '_macros/mymacro' as mymacro % }
  </code></pre>
  
  <p>When I attempt to run this I get this error:</p>
  
  <pre><code>Unable to find the template “_macros/mymacro”.
  </code></pre>
  
  <p>When running the same service/render code from a Controller, everything works. It seems like there is a difference with how paths are set or handled in a Controller vs. an event handler. I tried manually setting the template path based on <a href="http://buildwithcraft.com/docs/plugins/templates" rel="nofollow">this page</a>, but same results.</p>
  
  <p>Any thoughts on how to resolve this?</p>
  
  <p>Thanks in advance!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-15T15:45:44.520Z
Comments.Text: I'm not sure if this will help...but try renaming the .twig files as .html.
Comments.UserDisplayName: Aaron Berkowitz
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-16T15:59:21.763Z
Comments.Text: @aberkie - thanks for the suggestion, but that didn't help either. The strange thing is all of this works through a Controller, so my suspicion is that it has something to do with how paths are handled or loaded in Controllers vs. event handlers.
Comments.UserDisplayName: Nick Le Guillou
Tags: <plugin-development><templating><events><services>
Title: Issues with Twig template includes when rendering from a service called from a CP-triggered event
ThreadText: # Issues with Twig template includes when rendering from a service called from a CP-triggered event
  
  (Asked by: username_0 on 2015-02-13)
  
  I have a listener for the `entries.saveEntry`event that is calling a service, which generates an email from a rendered template. The template is attempting to import macros.
  
  MyaddonPlugin.php::init()
  
  ```
  craft()->on('entries.saveEntry', function(Event $event) {
      if ($event->params['isNewEntry']) {
          craft()->myaddon_emails->generateEmail($event->params['entry']);
      }
  });
  
  ```
  
  Myaddon\_EmailsService.php::generateEmail()
  
  ```
  $emailHtml = craft()->templates->render('myaddon/email');
  
  ```
  
  plugins/myaddon/templates/email.twig
  
  ```
  {% import '_macros/mymacro' as mymacro % }
  
  ```
  
  When I attempt to run this I get this error:
  
  ```
  Unable to find the template “_macros/mymacro”.
  
  ```
  
  When running the same service/render code from a Controller, everything works. It seems like there is a difference with how paths are set or handled in a Controller vs. an event handler. I tried manually setting the template path based on [this page](http://buildwithcraft.com/docs/plugins/templates), but same results.
  
  Any thoughts on how to resolve this?
  
  Thanks in advance!
  
  **Tags:** plugin-development, templating, events, services
  
  > **Comments:**
  > **username_1**: I'm not sure if this will help...but try renaming the .twig files as .html.  
  > **username_0**: @aberkie - thanks for the suggestion, but that didn't help either. The strange thing is all of this works through a Controller, so my suspicion is that it has something to do with how paths are handled or loaded in Controllers vs. event handlers.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-02-16. Score: 2
  
  Not 100% sure what is going wrong here, but here is what I would do to debug this on my end:
  
  1. Simply output the current path the template parser is using in the controller and event handler to see the difference in the values
  
     ```
     var_dump(craft()->path->getTemplatesPath());
     exit();
  
     ```
  2. If the values are different, you can override the value to do your parsing as your need it like so:
  
     ```
     // Get the current path and save it
     $oldPath = craft()->path->getTemplatesPath();
  
     // Set your desired template path
     craft()->path->setTemplatesPath('your/new/path');
  
     // Render your template and pass some data to it that will be parsed
     craft()->templates->render('your/template', $data = array());
  
     // Set the old path so Craft can resume as normal and you don't break stuff
     craft()->path->setTemplatesPath($oldPath);
  
     ```
  3. One other option is to try to include your macro like so:
  
     ```
     {% import 'myplugin/_macros/mymacro' as mymacro % }
  
     ```
  
  I basically do the exact same thing you are doing with [Postmaster for Craft](https://github.com/objectivehtml/Postmaster-for-Craft-CMS). I include my own macros, parse templates and strings during event handlers, it all works perfectly for me using the technique I outlined above. Feel free to take a look at my code if you want to see some more advanced examples of this.
  
  On a side note, I bet Postmaster will do exactly what you want to do in regards to sending an email for new entries. You should give it a shot, it has loads of additional features I bet you could use. No less, I do help this solve your problem either way. Sorry this wasn't really an official answer or a guaranteed fix, but I gave it my best shot without seeing all of your code.
  
  
  ---
  
  