how can I pass variable from child to the parent template?

686 views
Skip to first unread message

kenic...@gmail.com

unread,
Aug 9, 2007, 9:17:55 PM8/9/07
to Mako Templates for Python
Hi all,
Is there a way to pass variable from child to the parent template?
I have a base.mako file that displays the site's navigation tree. I
also have a profile.mako file that inherits from base.mako. I want to
pass a "current_page" variable from profile.mako to base.mako so that
the current page can be highlighted in the navigation tree. Is this
possible?

Michael Bayer

unread,
Aug 9, 2007, 11:24:22 PM8/9/07
to Mako Templates for Python
using inheritance, the best way to pass variables is to send arguments
to the body() function as follows:

# inheriting template
<%inherit file="foo.html"/>
<%page args="x, y, z"/>

x y z are ${x} ${y} ${z}


# inherited template
<%
x = 1
y = 2
z = 3
%>
${next.body(x=x, y=y, z=z)}

this is another thing that I have failed to place a specific example
for in the docs.

hope this helps.

- mike

On Aug 9, 9:17 pm, "keniche...@gmail.com" <keniche...@gmail.com>
wrote:

kenic...@gmail.com

unread,
Aug 10, 2007, 1:10:17 PM8/10/07
to Mako Templates for Python
Michael,
this will pass x, y, z from the inherited template to the inheriting
template right? what about vice versa? I want the inheriting file to
tell the inherited template what x, y and z is.

- Kenny

Michael Bayer

unread,
Aug 10, 2007, 2:39:44 PM8/10/07
to mako-d...@googlegroups.com
the best way to do vice versa right now is to define defs in your
inheriting template:

# inheriting template
<%inherit file="foo.html"/>

<%def name="stuff()"><%
return (3, 4, 5)
%></%def>

# inherited tempalte
<%
(x, y, z) = self.stuff()
%>

*or*

# inherited tempalte
<%
(x, y, z) = next.stuff()
%>

kenic...@gmail.com

unread,
Aug 13, 2007, 8:47:04 PM8/13/07
to Mako Templates for Python
Thanks Michael, that works.

On Aug 10, 11:39 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
> the best way to do vice versa right now is to define defs in your
> inheriting template:
>
> # inheriting template
> <%inherit file="foo.html"/>
>
> <%def name="stuff()"><%
> return (3, 4, 5)
> %></%def>
>
> # inherited tempalte
> <%
> (x, y, z) = self.stuff()
> %>
>
> *or*
>
> # inherited tempalte
> <%
> (x, y, z) = next.stuff()
> %>
>

Anton V. Belyaev

unread,
Aug 20, 2007, 11:16:46 AM8/20/07
to Mako Templates for Python
So simple! I invented much more tricky scheme :)

On 10 авг, 22:39, Michael Bayer <mike...@zzzcomputing.com> wrote:
> the best way to do vice versa right now is to define defs in your
> inheriting template:
>
> # inheriting template
> <%inherit file="foo.html"/>
>
> <%def name="stuff()"><%
> return (3, 4, 5)
> %></%def>
>
> # inherited tempalte
> <%
> (x, y, z) = self.stuff()
> %>
>
> *or*
>
> # inherited tempalte
> <%
> (x, y, z) = next.stuff()
> %>
>

Pascal Schaedeli

unread,
Mar 1, 2012, 11:28:06 PM3/1/12
to mako-d...@googlegroups.com
Excerpt: the best way to do vice versa right now [i.e. in 2007] is [...]: is there a better way today?

I can see how the def could be replaced by a block if appropriate in a given situation, but the only different is the "look". Is there a fundamentally newer/better concept that I could use (my objective is the same as in the question that initially started this thread)?

Thx. Pascal

Jinghui Niu

unread,
Jan 4, 2016, 6:58:49 PM1/4/16
to Mako Templates for Python
Hi Mike. It works as long as that variable is not used as a filename for an imported namespace. What if the variable you are passing from the child to the parent is the namespace file name? Then the namespace name definition will be hoisted to the beginning of that def and unaware of the returned value of next.stuff().
Reply all
Reply to author
Forward
0 new messages