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
Comment #2
jeffglenn CreditAttribution: jeffglenn commentedComment #3
cilefen CreditAttribution: cilefen commentedIs file 'includes/header.html.twig' defined in a hook_theme() implementation?
Comment #4
jeffglenn CreditAttribution: jeffglenn commentedNot 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!
Comment #5
cilefen CreditAttribution: cilefen commentedI 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 theinclude
statement used?Comment #6
jeffglenn CreditAttribution: jeffglenn commentedThanks 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!
Comment #7
cilefen CreditAttribution: cilefen commentedComment #8
cilefen CreditAttribution: cilefen commentedComment #9
cilefen CreditAttribution: cilefen commentedComment #10
lauriiiComment #12
mikeker CreditAttribution: mikeker as a volunteer commentedPart of the confusion (at least, for me...) is that anything included in a template via
extends
orincludes
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.Comment #13
matsbla CreditAttribution: matsbla commentedI had the same confusion. I think this needs to be better documented, or better; be in line with Twig documentation.
Comment #15
aaronsssp CreditAttribution: aaronsssp commentedFor 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:
Then put your Twig file in YOUR_MODULE/template/FILE_NAME.html.twig
Comment #16
Chi CreditAttribution: Chi commentedIt 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.
Comment #17
jmev CreditAttribution: jmev commentedRegarding storing includes in subfolders, this issue seems to touch upon this: https://www.drupal.org/node/2817103
Comment #19
karuna patel CreditAttribution: karuna patel as a volunteer and at SynapseIndia Outsourcing Pvt. Ltd. commentedif you want to include and render file write:
Comment #21
tranthanhthuy CreditAttribution: tranthanhthuy commentedcomposer require drupal/drush
>>> ../vendor/drush/drush/drush cr
Comment #25
capysara CreditAttribution: capysara at Bounteous commentedThanks @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' %}
Comment #30
tgoeg CreditAttribution: tgoeg commentedThis 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.
Comment #32
featuriz CreditAttribution: featuriz as a volunteer commentedHere 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
Code in /templates/mail/email--contact-form--mail--contact-us.html.twig
Code in /templates/mail/_body.html.twig
{{ body }}