Passing variables from a child template to a parent in Mako

After looking at Web.py’s own templating system, and considering Cheetah, I’ve ended up going with Mako for now. So far I’ve found it very straightforward, with some powerful-looking features to expand into in the future.

One of the great features is template inheritance - but I hit an issue recently with passing a variable from a child template to a parent. It’s not immediately obvious how to do this, but I found the answer on the Mako Google Groups archive (link here):

Child:


<%inherit file="meta.html"/>

<%def name="vars()">

	<% return (meta.id,) %>

</%def>

Parent:


<% (id,) = self.vars() %>

On a side note, I found out today that two of the Python libraries I’m using, Mako and SQLAlchemy, were coded by the same guy - Michael Bayer. Clever chap - not only are the libraries pretty impressive, but they are both well documented and he regularly contributes to the mailing lists for both projects. And he doesn’t even have a big internet head like some developers - though I tracked his blog down here:

http://techspot.zzzeek.org/