Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:38:31+00:00 2026-06-18T12:38:31+00:00

Most of the examples and tutorials for mako suggest you use files for templates.

  • 0

Most of the examples and tutorials for mako suggest you use files for templates.

How can I use inheritance, if the templates are stored in strings or in a database?

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):

from mako.template import Template

base = """
  <html>
    <body>

      <div class="header">
        <%block name="header"/>
      </div>

      Some body content ...

    </body>
  </html>
"""

index = """
  <%inherit file="base.html"/>

  <%block name="header">
    this is some header content
  </%block>

  this is the body content.
"""

base_template = Template(base)
index_template = Template(index)

print index_template.render()

Obviously this does not work.

The second template needs some way to know that base.html should be base_template.

This question has also been asked on the mako-discuss group in 2009:

https://groups.google.com/d/topic/mako-discuss/QiwkRu7rTFQ/discussion

This is what Michael Bayer wrote as an answer:

A TemplateLookup collection needs to be involved for templates to
access each other.

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”).

I could not yet figure out how to apply this and convert it to actual python code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:38:33+00:00Added an answer on June 18, 2026 at 12:38 pm

    First of all you need to add ${self.body()} to your base template, this will mark the place there the inheritor’s data will go. And then you can use TemplateLookup like so:

    from mako.template import Template
    from mako.lookup import TemplateLookup
    
    base = """
      <html>
        <body>
    
          <div class="header">
            <%block name="header"/>
          </div>
    
          ${self.body()}
    
        </body>
      </html>
    """
    
    index = """
      <%inherit file="base.html"/>
    
      <%block name="header">
        this is some header content
      </%block>
    
      this is the body content.
    """
    
    lookup = TemplateLookup()
    lookup.put_string("base.html", base)
    lookup.put_string("index.html", index)
    
    index_template = lookup.get_template("index.html")
    
    print index_template.render()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found that most tutorials use one big router. For example: https://github.com/thomasdavis/backboneboilerplate/blob/gh-pages/js/router.js Wouldn't it
I just wondered, why most Delphi examples use FillChar() to initialize records. type TFoo
What are the examples where we really need 3 Tier Architecture ? Can Most
Most of the tutorials that I've read only explain Cocos2D examples in the HelloWorld
Most of the examples and tutorials I have found for the Facebook SDK are
Are there any decent Android NDK examples and tutorials out there? Where can I
So from looking around at examples and tutorials, it seems the most common method
Most examples show how to redirect all subdomain traffic to a primary domain, maintaining
Most examples of creating remote branches involve pushing from a local branch Is there
In most examples that you find on the web when explicitly not using using,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.