<p>In my experience, this is the type of situation where you may want to consider having a second layout rather than opting for a more complicated inheritance scheme. If your layouts are somewhat complex and you want to keep your markup DRY I would suggest moving the more complex components of your layout into includes. </p>

<p>Also, when I get beyond the Layout &raquo; Template relationship, instead of defining blocks and extending the Template I opt for includes there as well. This makes my workflow efficient because I always consider the Template to be the base point, inheriting up into the Layout and inheriting down into any includes.</p>

<p>layout_1col.html</p>

<pre><code>&lt;!DOCTYPE html&gt;
{% include 'head' %}
&lt;body&gt;
  {% include 'header' %}
  &lt;main&gt;
    {% block 'main' %}{% endblock %}
  &lt;/main&gt;
  {% include 'footer' %}
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>layout_2col.html</p>

<pre><code>&lt;!DOCTYPE html&gt;
{% include 'head' %}
&lt;body&gt;
  {% include 'header' %}
  &lt;main&gt;
    {% block 'main' %}{% endblock %}
  &lt;/main&gt;
  &lt;aside&gt;
    {% block 'sidebar' %}{% endblock %}
  &lt;/aside&gt;
  {% include 'footer' %}
&lt;/body&gt;
</code></pre>

<p>MyTemplate.html</p>

<pre><code>{% extends 'layout_1col' %}
{% block 'main' %}
  &lt;p&gt;My content&lt;/p&gt;
  {% include 'mywidget' %}
{% endblock %}
</code></pre>

<p>By letting your layouts rely heavily on includes you can let them vary only where needed but keeping your inheritance logic simple and easily extended.</p>

CC BY-SA 3.0
2015-03-10T18:10:23.267Z
This is a very nice solution. Thanks for your feedback Michael.
julzmon
CC BY-SA 3.0
2015-03-10T11:39:38.063Z
2015-03-10T11:39:38.063Z
Michael Thomas
<p>I'm trying to figure out how to nest 3 templates and have them inherit the blocks. How can I sometimes reference the child.html when I need a wrapper and sometimes not reference it. Maybe I'm trying to do this the wrong way? Thank you for your feedback.</p>

<p>parent.html</p>

<pre><code>{% block contentBlock %}
{% endblock %}
</code></pre>

<p>child.html</p>

<pre><code>&lt;div&gt;    
{% extends "parent" %}
{% block content %}
&lt;/div&gt;
</code></pre>

<p>child-child.html</p>

<pre><code>{% extends "child" %}
{{ title }}
</code></pre>

<p>child2.html</p>

<pre><code>{% extends "parent.html" %}
{% block contentBlock %}
{{ title }}
</code></pre>

CC BY-SA 3.0
2015-03-10T09:17:27.603Z
Check out Twig's 'use' inheritance, it might be what you are looking for: http://twig.sensiolabs.org/doc/tags/use.html
Jamie Wade
<templating>
How do I nest multiple templates while keeping a wrapper for some pages
# How do I nest multiple templates while keeping a wrapper for some pages

(Asked by: username_0 on 2015-03-09)

I'm trying to figure out how to nest 3 templates and have them inherit the blocks. How can I sometimes reference the child.html when I need a wrapper and sometimes not reference it. Maybe I'm trying to do this the wrong way? Thank you for your feedback.
parent.html
{% block contentBlock %}
{% endblock %}

child.html
    
{% extends "parent" %}
{% block content %}


child-child.html
{% extends "child" %}
{{ title }}

child2.html
{% extends "parent.html" %}
{% block contentBlock %}
{{ title }}

**Tags:** templating

> **Comments:**
> **username_1**: Check out Twig's 'use' inheritance, it might be what you are looking for: http://twig.sensiolabs.org/doc/tags/use.html  

---

## 1 answers

### Answer by username_2 on 2015-03-10. Score: 3 | Accepted answer

In my experience, this is the type of situation where you may want to consider having a second layout rather than opting for a more complicated inheritance scheme. If your layouts are somewhat complex and you want to keep your markup DRY I would suggest moving the more complex components of your layout into includes. 
Also, when I get beyond the Layout » Template relationship, instead of defining blocks and extending the Template I opt for includes there as well. This makes my workflow efficient because I always consider the Template to be the base point, inheriting up into the Layout and inheriting down into any includes.
layout_1col.html

{% include 'head' %}

  {% include 'header' %}
  
    {% block 'main' %}{% endblock %}
  
  {% include 'footer' %}



layout_2col.html

{% include 'head' %}

  {% include 'header' %}
  
    {% block 'main' %}{% endblock %}
  
  
    {% block 'sidebar' %}{% endblock %}
  
  {% include 'footer' %}


MyTemplate.html
{% extends 'layout_1col' %}
{% block 'main' %}
  My content
  {% include 'mywidget' %}
{% endblock %}

By letting your layouts rely heavily on includes you can let them vary only where needed but keeping your inheritance logic simple and easily extended.

> **Comments:**
> **username_0**: This is a very nice solution. Thanks for your feedback Michael.  

---

