Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig_Error_Loader: Template is not defined - Drupal

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

like image 515
Labanino Avatar asked Dec 20 '25 03:12

Labanino


1 Answers

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" %}
like image 57
Kevin Wenger Avatar answered Dec 22 '25 17:12

Kevin Wenger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!