<p>I like to have the document skeleton with the head, scripts etc. in a _base.html template and if there's more than one basic page layout (think sidebars etc.) I additionally extend it with _layout-a.html, _layout-b.html, templates. Everything else I use includes for.</p>

<p>The templates are being transpiled down to PHP and cached, so there should not be any measurable performance influence really.</p>

CC BY-SA 3.0
2016-02-19T03:47:58.193Z
Thanks for the feedback! Sounds like it's a pretty common practice and there's virtually no performance hit. Thanks for confirming some suspicions for me.
Aaron Bushnell
CC BY-SA 3.0
2016-08-05T12:06:31.577Z
I would say it's actually *better* for maintenance to make use of inheritance wherever possible, that way you're not repeating portions of the same code across several different layouts (which can lead to bugs). Good to stick with the DRY principle.
Simon East
CC BY-SA 3.0
2016-02-19T03:09:49.333Z
2016-02-19T03:09:49.333Z
carlcs
<p>Here's a basic example of using nested <code>extends</code> in Twig. I figured this is a probably common use-case. You have a very general <code>_layout.html</code> that contains your common CSS, JS, meta tags, etc.</p>

<pre><code>{# _layout.html #}
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;{{title}}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  {% block content %}{% endblock %}
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Then you have a <code>_subpage.html</code> template that shapes the way all your subpages should look (main content area, sidebar, etc).</p>

<pre><code>{# _subpage.html #}
{% extends "_layout" %}

{% block content %}
  &lt;main&gt;
    {% block subpageContent %}{% endblock %}
  &lt;/main&gt;

  &lt;aside&gt;
    {% block subpageSidebar %}{% endblock %}
  &lt;/aside&gt;
{% endblock %}
</code></pre>

<p>Now I extend that <code>_subpage.html</code> in <code>about.html</code> and grab the blocks that belong to the <code>_subpage.html</code> template.</p>

<pre><code>{# about.html #}
{% extends "_subpage" %}
{% set title = "About" %}

{% block subpageContent %}
  &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae cupiditate suscipit, debitis accusantium tenetur impedit, veritatis id placeat, dolor hic temporibus architecto quas atque sunt officiis corrupti voluptatibus expedita. Assumenda!&lt;/p&gt;
{% endblock %}

{% block subpageSidebar %}
  &lt;li&gt;&lt;a href="#"&gt;Subpage Link 1&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="#"&gt;Subpage Link 2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="#"&gt;Subpage Link 3&lt;/a&gt;&lt;/li&gt;
{% endblock %}
</code></pre>

<p>Now I realize you <em>can</em> use nested extends in Twig, but I'm entirely sure if I <em>should</em>. Is this a maintenance nightmare? Does it take a performance hit to do this? What's the best practice for template relationships?</p>

<templating><performance>
Is using nested extends a wise idea or will it be a performance/maintenance nightmare?
# Is using nested extends a wise idea or will it be a performance/maintenance nightmare?

(Asked by: username_0 on 2016-02-19)

Here's a basic example of using nested extends in Twig. I figured this is a probably common use-case. You have a very general _layout.html that contains your common CSS, JS, meta tags, etc.
{# _layout.html #}



  
  {{title}}


  {% block content %}{% endblock %}



Then you have a _subpage.html template that shapes the way all your subpages should look (main content area, sidebar, etc).
{# _subpage.html #}
{% extends "_layout" %}

{% block content %}
  
    {% block subpageContent %}{% endblock %}
  

  
    {% block subpageSidebar %}{% endblock %}
  
{% endblock %}

Now I extend that _subpage.html in about.html and grab the blocks that belong to the _subpage.html template.
{# about.html #}
{% extends "_subpage" %}
{% set title = "About" %}

{% block subpageContent %}
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae cupiditate suscipit, debitis accusantium tenetur impedit, veritatis id placeat, dolor hic temporibus architecto quas atque sunt officiis corrupti voluptatibus expedita. Assumenda!
{% endblock %}

{% block subpageSidebar %}
  Subpage Link 1
  Subpage Link 2
  Subpage Link 3
{% endblock %}

Now I realize you can use nested extends in Twig, but I'm entirely sure if I should. Is this a maintenance nightmare? Does it take a performance hit to do this? What's the best practice for template relationships?

**Tags:** templating, performance


---

## 1 answers

### Answer by username_1 on 2016-02-19. Score: 4 | Accepted answer

I like to have the document skeleton with the head, scripts etc. in a \_base.html template and if there's more than one basic page layout (think sidebars etc.) I additionally extend it with \_layout-a.html, \_layout-b.html, templates. Everything else I use includes for.

The templates are being transpiled down to PHP and cached, so there should not be any measurable performance influence really.

> **Comments:**
> **username_0**: Thanks for the feedback! Sounds like it's a pretty common practice and there's virtually no performance hit. Thanks for confirming some suspicions for me.  
> **username_2**: I would say it's actually *better* for maintenance to make use of inheritance wherever possible, that way you're not repeating portions of the same code across several different layouts (which can lead to bugs). Good to stick with the DRY principle.  

---

