Rendering a twig file of module in a widget on a Dashboard

I’m working on developing a module for OpenEMR (v7.0.0.2), and part of my project involves creating a dashboard widget. I’ve successfully added the widget to the dashboard, but I’m encountering an issue with rendering a Twig template located within the module.

I understand that placing the Twig template in OpenEMR’s template directory would work, but my goal is to have the widget render the Twig template directly from the module directory. I’ve received an error message indicating that I’m not in OpenEMR’s template directory.

Is there a method or approach that would allow me to render the Twig file from within the module directory when I create the widget on the dashboard?

In your module listen to the TwigEnvironmentEvent::EVENT_CREATED. Then you can append / prepend the path you want for your module directory. Here is a sample snippet:

$GLOBALS['kernel']->getEventDispatcher()->addListener(TwigEnvironmentEvent::EVENT_CREATED, [$this, 'addTemplateOverrideLoader']);
public function addTemplateOverrideLoader(TwigEnvironmentEvent $event)
    {
        $twig = $event->getTwigEnvironment();
        $loader = $twig->getLoader();
        if ($loader instanceof FilesystemLoader) {
            $loader->prependPath(\dirname(__DIR__) . DIRECTORY_SEPARATOR . "templates" . DIRECTORY_SEPARATOR);
        }
}
2 Likes

i’ll give it a try :smiley:

TY

it solved my issue perfectly!!!

Thnaks for the help!