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