1

I have been searching around SE and the web in general for an answer to this. There seems to be plenty of solutions but none appear to be working for me.

I have a simple module that has a variable that gets called from a twig template in order to render a form.

The problem is no matter what I do I cannot get the module to render the HTML from within its own /templates directory.

The variable code is:

$oldMode = \Craft::$app->view->getTemplateMode();
\Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE);
$html = \Craft::$app->view->renderTemplate('../templates/_journey-planner.html');
\Craft::$app->view->setTemplateMode($oldMode);
return $html;

This is taken from the Craft docs. I have also used:

Event::on(View::class,
          View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS,
          function (RegisterTemplateRootsEvent $e) {
                $e->roots['journeyplannermodule'] = 'journeyplannermodule/src/templates';
            }
        );

Within the module's init() function (not sure if this is actually required or not).

What am I doing wrong and what do I need to do to simply display this template within the twig template?

I am calling the variable method with: {{ craft.journeyPlannerModule.getJourneyPlanner }}

Craft Version: 3.2.0

1

Now working. Turned out to be user error on my part. I had neglected the alias that I had created so the path to the template directory was incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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