I'm trying to use Drupal 8, with a custom theme, due big structure differences for my requirements I have a page--front.twig.html and a page.twig.html, I would like to create template parts as used in phrozn oder in a normal Symfony2 project, for example a footer.html.twig and a header.html.twig. These templates are saved under a subdirectory /parts/.

But when I call this templates as normal I just receive a string with the name of the template.

For example:

{# in page.html.twig or page--front.html.twig #}
{% include 'parts/footer.html.twig' %} 

Returns the file name as string:


It's possible to do that with Drupal 8?

7 Answers 7


You can't do relative paths, all paths must either be from the Drupal root or based on the namespaced path to your 'templates' directory: https://www.drupal.org/node/2143557.

I would avoid "from the Drupal root" and use namespaces, at least until https://www.drupal.org/node/2291449 is resolved.

{% include '@mytheme/parts/footer.html.twig' %}

Edit: If you want to pass additional arguments for loop or for checking some vars to your included twig template you should write in this way:

{% include '@mytheme/includes/form-block.html.twig' with {dog: true} %}

Edit: Now that https://www.drupal.org/node/2291449 has been committed you can also do:

{% include 'footer.html.twig' %}

See https://www.drupal.org/node/2369981 for why you saw the file name as a string instead of a helpful exception.

  • What about include and passing values? Suppose I would like to include image-formatter and passing the url null to avoid making linked image.
    – SaidbakR
    Commented Aug 7, 2016 at 0:44
  • The examples provided in this answer do not seem to work.
    – arnoldbird
    Commented Aug 15, 2017 at 19:21
  • 3
    Be aware that @mytheme resolves to the path of mytheme + the string /templates. In other words, @mytheme resolves to the mytheme/templates directory.
    – daniels
    Commented Nov 19, 2017 at 14:11
  • 1
    To further clarify the comment from @daniels. If your custom theme is named mytheme and it is located at 'themes/custom/mytheme' then the Twig namespace @mytheme resolves to 'themes/custom/mytheme/templates'. Including a template located at @mytheme/fragments/footer.html.twig means it will resolve to 'themes/custom/mytheme/templates/fragments/footer.html.twig'
    – ec0g
    Commented Mar 9, 2018 at 17:35

Use a full path to the theme directory. If you use the namespace you'll have to change it if you bring the code to another site or theme.

This should work:

{% include directory ~ '/templates/parts/footer.html.twig' %}
  • this is very handy when doing sub-themeing. Set this in your Parent theme, and then have a version in the child theme's template directory. Commented May 1, 2017 at 10:33
  • This is the only answer that worked for me.
    – arnoldbird
    Commented Aug 15, 2017 at 19:25
  • Note: directory does unexpected things if you're building a parent theme that is intended to be extended and overridden with subthemes.See drupal.org/project/drupal/issues/3049414 Commented Sep 29, 2022 at 18:58

Note you can pass variables after the with keyword :

{# template.html will have access to the variables from the current context and the additional ones provided #}
{% include 'template.html' with {'foo': 'bar'} %}

{% set vars = {'foo': 'bar'} %}
{% include 'template.html' with vars %}

See Twig documentation http://twig.sensiolabs.org/doc/tags/include.html


It's also possible to use Twig's use of an array to create a fallback when the file doesn't exist.

Eg. If footer.html.twig isn't available, defaultfooter.html.twig would be used.

{{ include([
]) }}
Combine this with the use of variables in the filenames and you can include different files depending on some sort of context.

Nb: I'm still trying to work out whether this is best practice or not.


Now that Add Twig template inheritance based on the theme registry, enable adding Twig loaders has been committed, you can also use the following line.

{% include 'footer.html.twig' %}
  • 1
    This doesn't work.
    – arnoldbird
    Commented Aug 15, 2017 at 19:20

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:

<?php 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

  • 1
    It is wrong information about path. To include file from module with theme hook, you need another way: {{ include('@yourModuleName/twigFileName.html.twig', { var1: var1Val, var2: var2Val }) }} Don't forget, that templates files must be in 'yourModuleName/templates/'.
    – Verter
    Commented Jul 11, 2019 at 10:23

From a module, a twig file can be included this way:

{% include '@my_module_name/path/to/file.html.twig' %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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