I am writing a custom module that uses several twig templates in my modules /templates directory. I want to use part templates (https://www.drupal.org/node/2634166) so that I could reference the following:

{% include directory ~ 'includes/spr_table_selectors.twig' %}

The problem is my include keeps referencing my main site theme (themes/mytheme/...). Is there an easy way I can reference my includes directory within my custom module?

1 Answer 1


There should be a change to the theming documentation in this case. Namespace for twig is always set to MYTHEME/templates or MYMODULE/templates.

The directory variable is to ACTIVETHEME/templates.

So if you look at how the Symphony folks have been do things, you will find out they have their template parts in a subdirectory named parts rather then a sibling directory named includes. Use the subdirectory will fixed your problem. BONUS you can use @ to call the namespace of your module or theme like this:

  {% include '@MYMODULE/parts/header.html.twig' %}

Remember to keep your template part name(s) unique from the templates in use by your Drupal install to avoid them being used as a full template item that overrides something provided by core or another module.

  • This worked perfectly. Thank you for the clarification! Commented Sep 1, 2016 at 18:55

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.