2

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?

4

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:

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

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! – Ian Carnaghan Sep 1 '16 at 18:55

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.