Drupal - Include a partial template

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.


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' %}

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

Tags:

Theming

8