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.