Conditioning behavior on sub template properties

17 views
Skip to first unread message

Chris Strass

unread,
Jul 5, 2007, 2:35:29 AM7/5/07
to Mako Templates for Python
Hi folks,

We're very excited about Mako, but are running into a little snag.
We've moving from cheetah, where we were defining properties in a
subtemplate that could control functionality in the base template.

Here is a simple example in Mako of the base template:

<%
should_write_section = True
%>

% if should_write_section:
<div>.....long content.....</div>
% end

What we would like to do is set this property in a template that
inherits from the base template. In cheetah, this was done via #attr
calls. So far in Mako it seems you can only override def calls when
you inherit. Is there a way to accomplish the same thing in Mako?

thanks,
Chris

Chris Strass

unread,
Jul 5, 2007, 2:43:19 AM7/5/07
to Mako Templates for Python
By the way, this is the pattern we used in Cheetah:

base template:

#attr $show_something = False

#if $show_something
<div>.....content...</div>
#end if

sub template:

#extends path.to.base_template

#attr $show_something = True

thanks,
Chris

Michael Bayer

unread,
Jul 5, 2007, 10:42:25 AM7/5/07
to mako-d...@googlegroups.com

yes %attr was something myghty had also, although over there they
were not mutable attributes. the closest thing Mako has to this is
probably to send the arguments along to the body() function. it
probably would even work with locals, i.e.

<%
should_write_section=True
%>

${next.body(**locals())}


inheriting template declares it in %page so that it gets copied into
the local namespace:

<% inherit file="/base.html"/>
<% page args="should_write_section=False"/>

Chris Strass

unread,
Jul 5, 2007, 11:55:39 PM7/5/07
to Mako Templates for Python
Hi Michael,

Thanks for the response. If I read your snippet correctly, it sends
the local variables from the base template to override the variables
in the sub template. What we are actually looking for though is the
other way around.

For instance, the equivalent of the following:

base:

<% page args="should_write_section=False" />

subtemplate:

<% inherit file="/base.html" vars="should_write_section=True" />

The last example obviously does not exist, but is in essence what we
are trying to do. I take it that Mako doesn't support overriding
variables in base templates?

thanks,
Chris

Michael Bayer

unread,
Jul 6, 2007, 10:59:20 AM7/6/07
to mako-d...@googlegroups.com

On Jul 5, 2007, at 11:55 PM, Chris Strass wrote:

>
> Hi Michael,
>
> Thanks for the response. If I read your snippet correctly, it sends
> the local variables from the base template to override the variables
> in the sub template. What we are actually looking for though is the
> other way around.
>
> For instance, the equivalent of the following:
>
> base:
>
> <% page args="should_write_section=False" />
>
> subtemplate:
>
> <% inherit file="/base.html" vars="should_write_section=True" />
>
> The last example obviously does not exist, but is in essence what we
> are trying to do. I take it that Mako doesn't support overriding
> variables in base templates?
>

the level of "sharing" in an inheritance relationship is, like a
python class, "self". so you can set and retrieve attributes on
"self" in that manner (or if not, i can make that work).

the version I illustrated using <%page> does accomplish the same
task, however. the variable's value is defined in the base class and
propigated through the body() call. the value in the <%page> tag is
merely a default and can be left blank to make it required.

Reply all
Reply to author
Forward
0 new messages