2

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

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.

2
  • Could you share example code that actually worked for you? Thanks! Commented Nov 12, 2021 at 15:47
  • Hi Gary, this is now a legacy project and the code has been archived. Unfortunately I do not have access too it any more, sorry,
    – jaypratt
    Commented Nov 15, 2021 at 10:18

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.