Issue #62 resolved

Have <%inherit> allow arguments, or other easy way to override/pass values

created an issue

Need a way to pass python values to the parent template. Possible solutions:

  • <%inherit> parameters e.g. <%inherit file="base.mako" args="current_tab='Settings'"/>
  • <%def> variant for values e.g. <%def name="current_tab" value="Settings" />

The current way to pass values to base template is by using a <%def>, e.g.:


<html> <% current_tab = self.current_tab() %> ... <li ${current_tab == 'Home' and 'id="current"' or ''>Home <li ${current_tab == 'Settings' and 'id="current"' or ''>Settings ... </html> <%def name="current_tab()"><% return "Home" %></%def>


<%inherit file="base.mako" /> <%def name="current_tab()"><% return "Settings" %></%def> ....

This syntax is pretty verbose, and calling self.current_tab() can accidentally result in rendered output.

Previous requests for this feature in the mailing list:

Comments (2)

  1. Michael Bayer repo owner

    theres an impoverished way to do this right now, like this:

    1. child template

    <%! foo='bar' %>

    <%inherit file="base"/>

    1. base template foo is ${}

    it would be quite easy to put a special accessor on Namespace called `attr`, which searches the full inheritance tree of modules for the attribute 'foo':

    foo is ${}

    but ive only thought about it for 30 seconds. how does that sound ?

  2. Log in to comment