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