Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-19T03:47:58.193Z
Answers.Text: 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.
Answers.UserDisplayName: Aaron Bushnell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-05T12:06:31.577Z
Answers.Text: 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.
Answers.UserDisplayName: Simon East
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-19T03:09:49.333Z
Answers.LastActivityDate: 2016-02-19T03:09:49.333Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <templating><performance>
Title: Is using nested extends a wise idea or will it be a performance/maintenance nightmare?
ThreadText: # 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 #}
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
  </head>
  <body>
    {% block content %}{% endblock %}
  </body>
  </html>
  
  ```
  
  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 %}
    <main>
      {% block subpageContent %}{% endblock %}
    </main>
  
    <aside>
      {% block subpageSidebar %}{% endblock %}
    </aside>
  {% 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 %}
    <p>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!</p>
  {% endblock %}
  
  {% block subpageSidebar %}
    <li><a href="#">Subpage Link 1</a></li>
    <li><a href="#">Subpage Link 2</a></li>
    <li><a href="#">Subpage Link 3</a></li>
  {% 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.  
  
  ---
  
  