Search code examples
pythonmako

Capture body content in Mako?


Is it possible to get the rendered body content from within a Mako template? What I mean is that I can display the body using ${self.body()}, but what if I want to do something to it first?


Solution

  • Apparently what I was looking for is the capture function. From the docs:

    The other way to buffer the output of a def or any Mako callable is by using the built-in capture function. This function performs an operation similar to the above buffering operation except it is specified by the caller.

    ${" results " + capture(somedef) + " more results "}
    

    Or in my case:

    <%
        body = capture(self.body)
        # etc.
    %>