Re: [Mako] Inheritance: passing page args anonymously

463 views
Skip to first unread message

Michael Bayer

unread,
Jul 26, 2012, 10:41:27 AM7/26/12
to mako-d...@googlegroups.com
**pageargs is the way to go, as it talks about at...ouch! it has it here: docs.makotemplates.org/en/latest/namespaces.html?highlight=pageargs#the-body-method but really should have it in the inheritance chapter too, sorry about that !  

the code you have below works for me:

from mako.lookup import TemplateLookup
l = TemplateLookup()

l.put_string("index.tmpl",
    """
<%page args="contacts"/>
<%inherit file="base.tmpl"/>

% for contact in contacts:
    ${contact}
% endfor
""")

l.put_string("base.tmpl",
    """"
<html>
    <body>
        <%block name="header">
            this is the header
        </%block>

        ${self.body(**pageargs)}

        <%block name="footer">
           this is the footer
        </%block>
    </body>
</html>""")


print l.get_template("index.tmpl").render(contacts=["one", "two", "three"])

On Jul 26, 2012, at 6:28 AM, Fritz wrote:

Hi,

I'm relatively new to Mako so please excuse a possibly stupid question.

I'm trying to have several templates inherit from a base template. All inheriting templates take different arguments as declared via page args and as passed via corresponding render calls. The base template should just pass those through anonymously or ignore the arguments completely.

Here is what I've tried:

## index.tmpl
<%page args="contacts"/>
<%inherit file="base.tmpl"/>

% for contact in contacts:
    ## Do something to print info for each contact
% endfor

## base.tmpl
## <%page args="contacts"/>
<html>
    <body>
        <%block name="header">
            this is the header
        </%block>

        ## ${self.body(contacts)}
        ${self.body(**pageargs)}
       ## ${self.body()}
       ## ${self.body(**context.kwargs)}

        <%block name="footer">
           this is the footer
        </%block>
    </body>
</html>

I had hoped this would work. Or one of the body calls commented out underneath. But I get the error that I'm not passing enough arguments to the render call. If I uncomment the page args directive in base.tmpl with 'contacts' being passed explicitly and then use the body() invocation with 'contacts' again specified explicitly then my page renders correctly. But that wouldn't be workable if base.tmpl got inherited from multiple other templates with different parameter signatures.

What am I doing wrong? (I assume what I'm trying to do is possible ...)

Thanks for any type of pointer in advance!

Cheers,

Fritz


--
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/-/lMqTxGKRQlsJ.
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.

Fritz

unread,
Jul 26, 2012, 11:01:03 AM7/26/12
to mako-d...@googlegroups.com
Thanks for taking a look first of all, Michael!

Hhhhhhmmmm ... if I run the snippet you've distilled from my code as a python script then it works for me as well. Thanks - had actually never tried that but should have.

However, if I embed it into CherryPy (which is really what I ultimately need to do) then I get this:

Error !

TypeError: render_body() takes exactly 1 argument (2 given)

e = sys.exc_info()[0] _render_error(template, context, e) else: callable_(context, *args, **kwargs) def _render_error(template, context, error): if template.error_handler: result = template.error_handler(context, error) if not result:
/Library/Python/2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py, line 824:
callable_(context, *args, **kwargs)
/Library/Python/2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py, line 798:
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
/Library/Python/2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py, line 766:
**_kwargs_for_callable(callable_, data))
/Library/Python/2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/template.py, line 412:
return runtime._render(self, self.callable_, args, data)
./test-sqlobject.py, line 36:
return index_tmpl.render(contacts)

You see in the last line how I'm invoking the index template. It gets created from index.tmpl as you expect.

Any ideas?

And thanks again,

Fritz

Michael Bayer

unread,
Jul 26, 2012, 11:02:52 AM7/26/12
to mako-d...@googlegroups.com
that needs to be a kw argument, (contacts=contacts)

--
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/-/HgDOtxYoFzsJ.

Fritz

unread,
Jul 26, 2012, 11:12:45 AM7/26/12
to mako-d...@googlegroups.com
Thanks a bunch! That did it. I must confess that I was blissfully unaware of that ... I guess I could have fooled around another few hours (which I did before I had asked in the first place).
Reply all
Reply to author
Forward
0 new messages