Twig_Error_Loader: Template is not defined - Drupal

11,205

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" %}
Share:
11,205
Labanino
Author by

Labanino

Updated on June 04, 2022

Comments

  • Labanino
    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 called header.html.twig.

    enter image description here

    What am I doing wrong? I am running Drupal in MAMP and OSX, El Capitan. Drupal 8.3.7

    • Admin
      Admin over 6 years
      Hi did you clear your cache?
    • Labanino
      Labanino over 6 years
      Yes, many times!
    • Admin
      Admin over 6 years
      Can 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
      Labanino over 6 years
      It 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
      Admin over 6 years
      So 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
      Admin over 6 years
      Overwriting 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
      Stanislav Agapov over 6 years
      Try to add / before directory: {% include '/' ~ directory ~ ... %}. Though, your initial code should work...
  • Himechi90
    Himechi90 over 5 years
    Thanks! I moved it into the templates folder and it works fine now! Noob mistake, still learning :)
  • Kevin Wenger
    Kevin Wenger over 5 years
    Happy it helps you ! There is no noob mistake, we always learn new thingy ^^. Happy codding to you.