Problem/Motivation

This is the error you get if a template was not found on the filesystem via an include statement in a twig template:

Twig_Error_Loader: Template "footer.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "footer.html.twig" in the Drupal theme registry.) in "themes/havanasquare/templates/pages/page.html.twig" at line 3.

The problem is that the file could not be found on the filesystem, not the "Drupal theme registry".

Proposed resolution

Help out our themers and explained what really happend.

Remaining tasks

User interface changes

Original Report

Hi,
I'm having issues with including files in my theme through: {% include 'filename.html.twig' %}

I have my page.html.twig template and I want to break out the header and footer of the template into separate files for easy updating. Simple enough.

I create my new file 'header.html.twig' in an 'includes' folder in the same directory as my 'page.html.twig' and in the page template I have {% include 'includes/header.html.twig' %}. When I save and refresh the page I get a white screen with this:

The website encountered an unexpected error. Please try again later.

Twig_Error_Loader: Template "footer.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "footer.html.twig" in the Drupal theme registry.) in "themes/havanasquare/templates/pages/page.html.twig" at line 3. in Twig_Loader_Chain->getCacheKey() (line 115 of vendor/twig/twig/lib/Twig/Loader/Chain.php).
Drupal\Core\Template\TwigEnvironment->getTemplateClass('footer.html.twig', NULL) (Line: 378)
Twig_Environment->loadTemplate('footer.html.twig', NULL) (Line: 286)
Twig_Template->loadTemplate('footer.html.twig', 'themes/havanasquare/templates/pages/page.html.twig', 3) (Line: 47)
__TwigTemplate_fea998e363960c4f3282a328eb416af5281fbd1ab4e1a4a6d5313635516d3b1a->doDisplay(Array, Array) (Line: 381)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/havanasquare/templates/pages/page.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 468)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 87)
__TwigTemplate_bc4fcddd7ff4eda4647e5ce2941a77f75b23b0d918da1ce4d489ab83bcbfc449->doDisplay(Array, Array) (Line: 381)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('themes/havanasquare/templates/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 652)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Every article I have found said that should work. I have also tried putting a direct path {% include directory ~ '/includes/header.html.twig' %} and other attempts with no luck. Been stuck on this for a while.

Any help would be appreciated!

Comments

jeffglenn created an issue. See original summary.

jeffglenn’s picture

Issue summary: View changes
cilefen’s picture

Is file 'includes/header.html.twig' defined in a hook_theme() implementation?

jeffglenn’s picture

Not that I know of and honestly I'm not quite sure what that means.
With Drupal 7 I could just include the file wherever <?php include('includes/header.php'); ?>

Is it different in D8?

Thank you!

cilefen’s picture

I just tested {% include directory ~ '/includes/header.html.twig' %} and it works perfectly well for me.

When using {% include directory ~ '/includes/header.html.twig' %}, where is header.html.twig in the directory structure of the theme, and in which file is the include statement used?

jeffglenn’s picture

Thanks so much for the help. I finally got it working. I guess I wasn't defining the entire path like I thought I was.

I was thinking that if the page.html.twig was in a directory and you created a new folder in that directory called 'includes' you could just write {% include 'includes/header.html.twig' %}. But apparently that's NOT the case and you have to define the entire path from the theme root. Even if the include file is in the same directory as your template file you still have to define the entire path.

Anyway long story short I was missing a few folders. This is what works: {% include directory ~ '/templates/pages/includes/header.twig' %}

But I'm interested to know why this is working for others: http://drupal.stackexchange.com/a/141104

Thank you again!

cilefen’s picture

Status: Active » Fixed
cilefen’s picture

Title: Having trouble with Twig Includes » Error when a template is not found via an "include" is confusing
Category: Support request » Bug report
Issue summary: View changes
Status: Fixed » Active
cilefen’s picture

Issue summary: View changes
lauriii’s picture

Version: 8.2.1 » 8.3.x-dev

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mikeker’s picture

Part of the confusion (at least, for me...) is that anything included in a template via extends or includes must be registered in Drupal's theme registry. And that flies in the face of the Twig documentation of those functions (see includes and extends) which says that you can just include any file you want.

matsbla’s picture

I had the same confusion. I think this needs to be better documented, or better; be in line with Twig documentation.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

aaronsssp’s picture

For Drupal 8

Including a twig file in the theme
when you do an {% include 'FILE_NAME.html.twig' %} Drupal expects that you FILE will be located in your current Theme Directory.
Just put it there and it's going to work.

Including a twig file in your Module

If you want to add it in a MODULE, you have to declare it in the THEME Function.
In your .module file just add the following:

function YOUR_MODULE_NAME_theme($existing, $type, $theme, $path) {
  'FILE_NAME' => [
      'variables' => [
      ],
      'template' => 'FILE_NAME',
    ],

Then put your Twig file in YOUR_MODULE/template/FILE_NAME.html.twig

Chi’s picture

Drupal expects that you FILE will be located in your current Theme Directory.

It works, but it is against the common practice to have templates in templates directory.
I propose we document somewhere the solution described in note #5.

jmev’s picture

Regarding storing includes in subfolders, this issue seems to touch upon this: https://www.drupal.org/node/2817103

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

karuna patel’s picture

if you want to include and render file write:

{% include '../_templates/FILE_NAME.html.twig' %}

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tranthanhthuy’s picture

composer require drupal/drush
>>> ../vendor/drush/drush/drush cr

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

capysara’s picture

Thanks @jeffglenn!

It's weird because I have some include statements for templates that are in the same directory and {% include 'FILE_NAME.html.twig' %} works fine, but in another instance, it didn't. #6 worked for me: {% include directory ~ '/templates/page/FILE_NAME.html.twig' %}

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tgoeg’s picture

This can now also be caused by twig 2.15.2 and does not really mean the file could not be found but that it does not have an allowed extension defined in services.yml
The error message is the same and also confusing, in another way.

See https://git.drupalcode.org/project/drupal/-/commit/82a7d4dd3077ef16b69f2... for details.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

featuriz’s picture

Here is my working folder structure:

/templates/mail/email_template.html.twig
/templates/mail/email--contact-form--mail--contact-us.html.twig
/templates/mail/_body.html.twig

Code in /templates/mail/email_template.html.twig

<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<header>Header</header>
	<main>{% block content %}{% endblock %}</main>
	<footer>Footer</footer>
</body>
</html>

Code in /templates/mail/email--contact-form--mail--contact-us.html.twig

{% extends directory ~ '/templates/mail/email_template.html.twig' %}
{% block content %}
	{% include(directory ~ '/templates/mail/_body.html.twig') %}
{% endblock %}

Code in /templates/mail/_body.html.twig
{{ body }}

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.