Answers.Body: <p>Answer from <a href="https://craftcms.stackexchange.com/users/5557/robin-schambach">@robin-schambach</a> </p>
  
  <pre><code>It's not possible the way you do it... or the way you are trying to do it
  During site requests only templates from your template folder can be loaded
  otherwise it would be a high security issue
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-22T09:18:12.293Z
Answers.LastActivityDate: 2019-02-22T09:18:12.293Z
Answers.UserDisplayName: Jorge Anzola
Body: <p>I'm creating a module that contains some predefined templates. How can I load them directly out of the vendor folder? I've tried doing </p>
  
  <pre><code>Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function(RegisterTemplateRootsEvent $event) {
      if (is_dir($baseDir = $this-&gt;getBasePath().DIRECTORY_SEPARATOR.'templates')) {
          $event-&gt;roots[$this-&gt;id] = $baseDir;
      }
  });
  </code></pre>
  
  <p>That part seems to be working, cause if I dump it, I get:</p>
  
  <pre><code>[
      'my-module' =&gt; '/app/modules/mymodule/src/templates'
  ]
  </code></pre>
  
  <p>In my module, I have a <code>templates/test.twig</code></p>
  
  <p>But when I do <code>mysite.com/test</code>, I still get a 404.</p>
  
  <p>I've also tried a different approach, in the routes.php, I put:</p>
  
  <pre><code>'test' =&gt; ['template' =&gt; '@modules/my-module/test'],
  </code></pre>
  
  <p>Still nothing.</p>
  
  <p>Is there something I could differently?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-22T14:22:49.743Z
Comments.Text: I'm not sure this is what you're looking for, but did you already tried something like `Craft::$app->getView()->renderTemplate('_mySection/entry', array_merge($variables))`? Using this, you could get your template content generated within your plugin.
Comments.UserDisplayName: Romain P.
Tags: <plugin-development><templating><modules>
Title: How can I load the templates from a module?
ThreadText: # How can I load the templates from a module?
  
  (Asked by: username_0 on 2019-02-21)
  
  I'm creating a module that contains some predefined templates. How can I load them directly out of the vendor folder? I've tried doing
  
  ```
  Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function(RegisterTemplateRootsEvent $event) {
      if (is_dir($baseDir = $this->getBasePath().DIRECTORY_SEPARATOR.'templates')) {
          $event->roots[$this->id] = $baseDir;
      }
  });
  
  ```
  
  That part seems to be working, cause if I dump it, I get:
  
  ```
  [
      'my-module' => '/app/modules/mymodule/src/templates'
  ]
  
  ```
  
  In my module, I have a `templates/test.twig`
  
  But when I do `mysite.com/test`, I still get a 404.
  
  I've also tried a different approach, in the routes.php, I put:
  
  ```
  'test' => ['template' => '@modules/my-module/test'],
  
  ```
  
  Still nothing.
  
  Is there something I could differently?
  
  **Tags:** plugin-development, templating, modules
  
  > **Comments:**
  > **username_1**: I'm not sure this is what you're looking for, but did you already tried something like `Craft::$app->getView()->renderTemplate('_mySection/entry', array_merge($variables))`? Using this, you could get your template content generated within your plugin.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-02-22. Score: 2 | Accepted answer
  
  Answer from [@robin-schambach](https://craftcms.stackexchange.com/users/5557/robin-schambach)
  
  ```
  It's not possible the way you do it... or the way you are trying to do it
  During site requests only templates from your template folder can be loaded
  otherwise it would be a high security issue
  
  ```
  
  
  ---
  
  