1

I've read numerous answers to similar questions here but I can't for the life of me work out how to do this seemingly simple thing. I simply want to include a Twig template (theme_name/templates/system/footer.twig.html) from another Twig template in my custom theme (theme_name/templates/system/page.twig.html).

In my page.html.twig I've tried all of the following (obviously with theme_name as the actual theme name):

{% include 'themes/custom/theme_name/templates/system/footer.html.twig' %}
{% include 'footer.html.twig' %}
{% include 'system/footer.html.twig' %}
{% include directory ~ '/system/footer.html.twig' %}
{% include '/' ~ directory ~ '/system/footer.html.twig' %}
{% include "@theme_name/system/footer.html.twig" %}
{% include "/var/www/html/web/themes/custom/theme_name/system/footer.html.twig" %}

Each of these produces the following error, with the template varying on the method I've tried:

Twig_Error_Loader: Template "themes/custom/theme_name/templates/footer.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/custom/theme_name/templates/footer.html.twig" in the Drupal theme registry.) in "themes/custom/theme_name/templates/homepage/page--front.html.twig" at line 177. in Twig_Loader_Chain->getCacheKey() (line 115 of /var/www/html/vendor/twig/twig/lib/Twig/Loader/Chain.php).

I've even tried using hook_theme in my theme_name.theme file to no avail.

<?php

function my_theme_theme() {
  return [
    'footer' => [
      'variables' => [],
    ],
  ];
}
2

No need to set up hook_theme, Just include like this:

{% include '@theme_name/system/footer.html.twig' with content %}

this will include that template in your main template. The variables will need to be available from the main template.

The "with" statement sets up the variables being sent to the sub-template. See http://twig.sensiolabs.org/doc/tags/include.html

  • Many thanks for your answer but unfortunately that doesn't work either. I'm genuinely at a loss as to why I can't get this to work. – Jamie Hollern Oct 26 '16 at 19:37
  • 1
    I tested it and it works. Maybe you have a conflict with your footer name, have you tried another name? myfooter.html.twig or some such? – oknate Oct 26 '16 at 19:53
  • The error is because you don't have a template at themes/custom/theme_name/templates/footer.html.twig, so the theme registry can't find the template, try removing the my_theme_theme function, or add that template. – oknate Oct 26 '16 at 20:28
  • It turns out that this was an issue between my host machine and my virtual machine not sharing files correctly. Thanks for the help, it's very much appreciated. – Jamie Hollern Oct 26 '16 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.