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?

  • 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.
    Commented Feb 22, 2019 at 14:22

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.