Multiple inheritance with blocks

166 views
Skip to first unread message

William

unread,
Jan 15, 2013, 9:22:35 AM1/15/13
to mako-d...@googlegroups.com
Hi,

I want to do this:

base.html:
Stuff
<%block name="start"/>
Other stuff

middle.html:
<%inherit file="base.html"/>
Stuff
<%block name="start">
<%block name="body"/>
</%block>

last.html:
<%inherit file="middle.html"/>
<%block name="body">
Hi
</%block>

Is this possible?  I seem to be getting errors any way I try to do this.

Michael Bayer

unread,
Jan 15, 2013, 12:10:20 PM1/15/13
to mako-d...@googlegroups.com
First note that this is not "multiple" inheritance, which means multiple sibling bases, this is "multilevel" inheritance.

Here, the execution is: base.html-> start-> middle.html->start, middle.html->body, which is empty. The "body" block inside of "last.html" is not at the same level as "body" inside of middle so there is no automatic relationship between these (inheritance also does not propagate inside of nested blocks in any case).

You can invoke last.html->body explicitly via "next":

from mako.lookup import TemplateLookup

l = TemplateLookup()

l.put_string("base.html", """
Stuff
<%block name="start"/>
Other stuff

""")

l.put_string("middle.html", """
<%inherit file="base.html"/>
Stuff
<%block name="start">

${next.body()}

</%block>

""")

l.put_string("last.html", """
<%inherit file="middle.html"/>

<%block name="body">
Hi
</%block>

""")


print l.get_template("last.html").render()

William

unread,
Jan 15, 2013, 2:34:57 PM1/15/13
to mako-d...@googlegroups.com
Thank you for the reply.  I think that ${next.body()} puts the whole of last.html in there, right?  I should have given a more detailed example the first time.  So my real question: Is the following possible?  Can I do this with defs or something else?  ${next.body()} doesn't let me split up last.html the way I need.

base.html:
Stuff
<%block name="start"/>
Other stuff
<%block name="start2"/>
done

middle.html:
<%inherit file="base.html"/>
Stuff
<%block name="start">
things
<%block name="body"/>
more things
</%block>
<%block name="start2">
stuff
<%block name="body2"/>
other suff
</%block>

last.html:
<%inherit file="middle.html"/>
<%block name="body">
Hi
</%block>
<%block name="body2">Hi again</%block>

Michael Bayer

unread,
Jan 15, 2013, 3:01:12 PM1/15/13
to mako-d...@googlegroups.com
oh, right "body" is a special name.  So here use a different name.  When you're inside of "middle.html", "next" is the namespace of all the top level defs and blocks inside of last.html.    

l.put_string("middle.html", """
<%inherit file="base.html"/>
Stuff
<%block name="start">

${next.inner_body()}

</%block>

""")

l.put_string("last.html", """
<%inherit file="middle.html"/>

not the inner_body

<%block name="inner_body">
Hi
</%block>

""")


works on this end.


--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mako-discuss/-/k8pfu_yIJHoJ.
To post to this group, send email to mako-d...@googlegroups.com.
To unsubscribe from this group, send email to mako-discuss...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.

William

unread,
Jan 15, 2013, 3:29:04 PM1/15/13
to mako-d...@googlegroups.com
Aha!  That is it!  All examples I ever saw were using that for defs, not blocks, so I never assumed you could use it for blocks as well.

Thank you very much - I never would have figured that out on my own.
Reply all
Reply to author
Forward
0 new messages