title: Nested django templates
body: <p>This seems like a pretty basic thing to do but although I've been using Django for around a year, I've never run into this scenario yet.</p>
  
  <p>In a lot of templating/web frameworks, template inheritance works a bit differently, in that usually it behaves more like wrappers, so if you have childtemplate.html, parenttemplate.html, and grandparenttemplate.html, then the finally rendering usually looks something like:</p>
  
  <pre><code>grandparent header
      parent header
          child header
          child content
      parent content
      parent footer
  grandparent content
  grandparent footer
  </code></pre>
  
  <p>That's not exactly how it works in Django but I'm wondering how to implement it.</p>
  
  <p>Specifically I have my "child" template, let's just say it's <code>foo.html</code>. <code>foo.html</code> optional gets a variable <code>parent_template</code> or defaults to "base.html"</p>
  
  <pre><code>{% extends parent_template|default:"base.html" %}
  
  {% block content %}
  I am a foo and my title is {{ foo.title }}
  {% endblock content %}
  </code></pre>
  
  <p>So, this is where I run into a snag. If <code>parent_template</code> is a template, that template should wrap the contents of <code>foo.html</code> and then place the result into <code>base.html</code>:</p>
  
  <pre><code>{% extends "base.html" %}
  
  {% something_magical_here %}
  &lt;div&gt;parent header&lt;/div&gt;
  # content of foo.html
  &lt;div&gt;parent footer&lt;/div&gt;
  {% end_something_magical_here %}
  </code></pre>
  
  <p>And then in <code>base.html</code>:</p>
  
  <pre><code>&lt;html&gt;
  ... snip ...
  &lt;div id="content"&gt;
  {% something_else_magical %}
  # content of parent_template rendering, or just foo.html if no parent_template given
  {% end_something_else_magical %}
  </code></pre>
  
  <p>Which should render as</p>
  
  <pre><code>&lt;html&gt;
  ... snip ...
  &lt;div id="content"&gt;
  &lt;div&gt;parent header&lt;/div&gt; 
  I am a foo and my title is Bar
  &lt;div&gt;parent footer&lt;/div&gt;
  </code></pre>
  
  <p>if parent_template is set and</p>
  
  <pre><code>&lt;html&gt;
  ... snip ...
  &lt;div id="content"&gt;
  I am a foo and my title is Bar
  </code></pre>
  
  <p>if it is not.</p>
  
  <p>I hope my problem is clear: I need to (optionally) wrap a template in a parent template, and then send the results of that to the <code>base.html</code> template. </p>
  
  <p>Normally, something like this might work:</p>
  
  <pre><code>#foo.html
  {% extends "parent.html" %}
  {% block child_content %}
  I am a foo and my title is {{ foo.title }}
  {% endblock child_content %}
  
  #parent.html
  {% extends "base.html" %}
  
  {% block content %}
  parent header
  {% block child_content %}{% endblock child_content %}
  parent content
  parent footer
  
  #base.html
  base header
  {% block content %}{% endblock content %}
  base content
  base footer
  </code></pre>
  
  <p>However, since the parent_template could be blank, then sometimes base.html will be getting just the <code>child_content</code> block, not the <code>content</code> block.</p>
  
  <p>Also, I'd like to be able to do this without having to create a bunch of sub-blocks (what if I decide that the <code>foo</code> application should have its own <code>/foo/base.html</code> that then calls <code>/base.html</code>)?</p>
  
  <p>Any ideas?</p>