title: How to use inheritance with mako using non-file based templates?
body: <p>Most of the examples and tutorials for mako suggest you use files for templates.</p>
  
  <p>How can I use inheritance, if the templates are stored in strings or in a database?</p>
  
  <p>As a starting point I am trying to create a variable based inheritance example based on the file-based inheritance example from the mako website (which could be easily converted to a database-based example afterwards): </p>
  
  <pre><code>from mako.template import Template
  
  base = """
    &lt;html&gt;
      &lt;body&gt;
  
        &lt;div class="header"&gt;
          &lt;%block name="header"/&gt;
        &lt;/div&gt;
  
        Some body content ...
  
      &lt;/body&gt;
    &lt;/html&gt;
  """
  
  index = """
    &lt;%inherit file="base.html"/&gt;
  
    &lt;%block name="header"&gt;
      this is some header content
    &lt;/%block&gt;
  
    this is the body content.
  """
  
  base_template = Template(base)
  index_template = Template(index)
  
  print index_template.render()
  </code></pre>
  
  <p>Obviously this does not work. </p>
  
  <p>The second template needs some way to know that base.html should be base_template.</p>
  
  <p>This question has also been asked on the mako-discuss group in 2009:</p>
  
  <p><a href="https://groups.google.com/d/topic/mako-discuss/QiwkRu7rTFQ/discussion" rel="nofollow">https://groups.google.com/d/topic/mako-discuss/QiwkRu7rTFQ/discussion</a></p>
  
  <p>This is what Michael Bayer wrote as an answer:</p>
  
  <blockquote>
    <p>A TemplateLookup collection needs to be involved for templates to<br>
    access each other.   </p>
    
    <p>Any template can be associated with a lookup using either the
    "lookup=some_lookup" keyword argument sent to Template, or by creating
    the Template strings directly with the lookup using lookup.put("some
    template name", "your template").</p>
  </blockquote>
  
  <p>I could not yet figure out how to apply this and convert it to actual python code. </p>