<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>

CC BY-SA 4.0
2019-02-22T09:18:12.293Z
2019-02-22T09:18:12.293Z
Jorge Anzola
<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>

CC BY-SA 4.0
2019-02-22T14:22:49.743Z
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.
Romain P.
<plugin-development><templating><modules>
How can I load the templates from a module?
# 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 
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


---

