title: Rendering Jinja2 inheritance
body: <p>I want to use the concept of inheritance in jinja2 to separate different parts of the configuration into multiple separate child templates.</p>
  
  <p>Ultimately, make it work for more complex hierarchy, something like:</p>
  
  <pre><code>parent.jj2
    |_child1.jj2
    |   |_child11.jj2
    |   |
    |   |_child12.jj2
    |
    |_child2.jj2
        |_child21.jj2
        |
        |_child22.jj2
  </code></pre>
  
  <p>In this example I use only 2 child templates:</p>
  
  <p><strong>parent.jj2 template:</strong></p>
  
  <pre><code>## Main file header
  {% block child1 %}{% endblock %}
  ##
  {% block child2 %}{% endblock %}
  ## Main file tail
  </code></pre>
  
  <p><strong>child1.jj2 template:</strong></p>
  
  <pre><code>{% extends "parent.jj2" %}
  {% block child1 %}
  {% for i in list %}
  child1 line {{ i }}
  {% endfor %}
  {% endblock %}
  </code></pre>
  
  <p><strong>child2.jj2 template:</strong></p>
  
  <pre><code>{% extends "parent.jj2" %}
  {% block child2 %}
  {% for i in list %}
  child2 line {{ i }}
  {% endfor %}
  {% endblock %}
  </code></pre>
  
  <p>I use the following render, where child template are rendered separately, but it doesn't produce the desired result:</p>
  
  <p><strong>Used jinja2 Loader:</strong></p>
  
  <pre><code>list1 = \
      [1,2,3]
  
  list2 = \
      ['a','b','c']
  
  loader = jinja2.FileSystemLoader(os.getcwd())
  jenv = jinja2.Environment(loader=loader, trim_blocks=True,lstrip_blocks=True)
  
  template = jenv.get_template('child1.jj2')
  print template.render(list=list1)
  
  template = jenv.get_template('child2.jj2')
  print template.render(list=list2)
  </code></pre>
  
  <p>How to call the render in a such way that I have the main template rendered with the child blocks in a single file?</p>
  
  <p><strong>Desired result:</strong></p>
  
  <pre><code>## Main file header
  child line1 1
  child line1 2
  child line1 3
  ##
  child line1 a
  child line1 b
  child line1 c
  ## Main file tail
  </code></pre>
  
  <p><strong>Current outcome:</strong> </p>
  
  <pre><code>## Main file header
  child line1 1
  child line1 2
  child line1 3
  ##
  ## Main file tail
  
  ## Main file header
  ##
  child line1 a
  child line1 b
  child line1 c
  ## Main file tail
  </code></pre>