4

I'm trying to use Drupal 8, with an own 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 wenn 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?

felipep
  • 2,442
  • 1
  • 28
  • 35

3 Answers3

5

You can include any part of your template files like this

{% include directory ~ '/parts/footer.html.twig' %}

or this

{% include '@mytheme/parts/footer.html.twig' %}

I strongly recommend you to create a reusable layout for pages that will give you greater flexibility when dealing with more pages and variants.

{# filename: page-layout.html.twig #}

{% block content%}
{{ page.content }}
{% endblock%}

{% block footer%}
{% include '@mytheme/parts/footer.html.twig' %}
{% endblock%}

So you can do something like this in another page

{# filename: page--front.html.twig #}
{% block footer%}
<div> I want to handle a different footer in here</div>
{% endblock%}

Finally, I found really helpful to dig into suggestions array and see what Drupal is trying to use.

Cheers.

Oscar Nevarez
  • 994
  • 12
  • 24
4

it's possible using the name of the template in the path

{% include '@mytheme/parts/footer.html.twig' %}

thanks to https://drupal.stackexchange.com/questions/141066/drupal-8-include-part-template

Community
  • 1
  • 1
felipep
  • 2,442
  • 1
  • 28
  • 35
1

Now that https://www.drupal.org/node/2291449 has been committed you can also do:

{% include 'footer.html.twig' %}
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
  • May you write an answer also for http://drupal.stackexchange.com/questions/141066/drupal-8-include-part-template, possibly more extensive? – apaderno Jul 13 '16 at 17:25
  • @kiamlaluno sure .tnx – Yuseferi Jul 13 '16 at 20:49
  • When i use something like this: {% include 'site-header.html.twig' %} twig throws me error "Twig_Error_Loader: Template "site-header.html.twig" is not defined" it works with @themename/site-header.html.twig, but i find it ugly and phpstorm doesnt know how to resolve it – jmwierzbicki Jul 18 '17 at 14:47