22

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:

parts/footer.html.twig

It's possible to do that with Drupal 8?

6 Answers 6

37

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.

4
  • 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
    Aug 7, 2016 at 0:44
  • The examples provided in this answer do not seem to work.
    – arnoldbird
    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
    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
    Mar 9, 2018 at 17:35
22

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' %}
3
  • 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. May 1, 2017 at 10:33
  • This is the only answer that worked for me.
    – arnoldbird
    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 Sep 29, 2022 at 18:58
7

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

5

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([
  '@yourthemename/parts/footer.html.twig',
  '@yourthemename/parts/defaultfooter.html.twig'
]) }}
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.

5

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
  • 1
    This doesn't work.
    – arnoldbird
    Aug 15, 2017 at 19:20
2

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
  • 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
    Jul 11, 2019 at 10:23

Your Answer

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

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