Twig_Error_Loader: Template is not defined - Drupal
When using the include
or extends
of Twig with Drupal 8, you have to specify the theme/modules namespace.
Here the complete explanation from Drupal.org: https://www.drupal.org/node/2143557
Plus, you should add your partials files into the templates folder of your custom theme. It's a best practice.
Usage example
{% include "@mytheme/partials/favicons.html.twig" %}
Labanino
Updated on June 04, 2022Comments
-
Labanino almost 2 years
I created an
include
in Drupal:{% include directory ~ '/partials/header.html.twig' %}
but I am getting this error:
Twig_Error_Loader: Template "themes/custom/mytheme/partials/header.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/custom/mytheme/partials/header.html.twig" in the Drupal theme registry.) in "themes/custom/mytheme/page--front.html.twig" at line 1. in Twig_Loader_Chain->getCacheKey() (line 115 of vendor/twig/twig/lib/Twig/Loader/Chain.php).
According to Drupal and Twig documentation, I am doing it the right way.
I have a directory called
partials
and a file calledheader.html.twig
.What am I doing wrong? I am running Drupal in MAMP and OSX, El Capitan. Drupal 8.3.7
-
Admin over 6 yearsHi did you clear your cache?
-
Labanino over 6 yearsYes, many times!
-
Admin over 6 yearsCan you add on the topic the version of drupal plz. And did you attach your theme to the registry, is a new module or did you overwriting core files?
-
Labanino over 6 yearsIt is Drupal 8.3.7 I have not attached my theme to the registry, not a new module and I have not touched the core files.
-
Admin over 6 yearsSo you got 2 solution create a new module make a hook to attach regitry to your template or overwriting the you using template but this mean you can't update the template or work on overwriting the core the first solution is the best, regards. Solution of your problem here drupal.stackexchange.com/questions/25846/…
-
Admin over 6 yearsOverwriting core, mean you have to take care of drupal updates because if the template add some update in queue, it mean you lost all the change you did on this template.
-
Stanislav Agapov over 6 yearsTry to add
/
beforedirectory
:{% include '/' ~ directory ~ ... %}
. Though, your initial code should work...
-
-
Himechi90 over 5 yearsThanks! I moved it into the templates folder and it works fine now! Noob mistake, still learning :)
-
Kevin Wenger over 5 yearsHappy it helps you ! There is no noob mistake, we always learn new thingy ^^. Happy codding to you.