19

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?

31

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: 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 Aug 7 '16 at 0:44
  • The examples provided in this answer do not seem to work. – arnoldbird Aug 15 '17 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 '17 at 14:11
  • 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 '18 at 17:35
16

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. – Andrew Killen May 1 '17 at 10:33
  • This is the only answer that worked for me. – arnoldbird Aug 15 '17 at 19:25
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

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

4

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 Aug 15 '17 at 19:20
1

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

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.