Dynamic inheritance

226 views
Skip to first unread message

alextheinsomniac

unread,
Mar 25, 2008, 11:48:09 PM3/25/08
to Mako Templates for Python
I wish to get a template to inherit based on a variable set in the
calling python.

ie:

page.py:
mytemplate = Template(filename='/docs/mytmpl.txt')
print mytemplate.render(parentTemplate="parent.mako")


/docs/mytmpl.txt
<%inherit file="${parentTemplate}"/>

I get "global name 'parentTemplate' is not defined"
I've tried defining a function and that has the same problem.
Can anyone point me in the direction of a working example of dynamic
inheritance?
Thanks

Michael Bayer

unread,
Mar 26, 2008, 10:59:45 AM3/26/08
to mako-d...@googlegroups.com

the <%inherit> tag is evaluated outside of the body of the template,
so the automatic context resolution doesn't occur there. try this
instead:

<%inherit file="${context.get('parentTemplate')}"/>


alextheinsomniac

unread,
Mar 26, 2008, 4:02:06 PM3/26/08
to Mako Templates for Python
That sorted it!
Thank you Michael.

I'm somewhat new to Mako and although I've found the documentation to
be clear it would be useful to see some other examples. Are there any
other resources on the internet or print. In fact are there any open
source applications using Mako that you would recommend for their
style?

Thanks again for your help.
Message has been deleted

Michael Bayer

unread,
Apr 8, 2008, 2:03:46 AM4/8/08
to Mako Templates for Python
we're pretty new so theres not exactly a huge amount of public
examples out there. the websites for both mako and sqlalchemy are in
the SVN repositories of those projects and both also use Mako to
generate their docs. Python.org's build system is downloadable too
but only seems to have a single template.
Reply all
Reply to author
Forward
0 new messages