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 displa开发者_JAVA百科y the body using ${self.body()}
, but what if I want to do something to it first?
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.
%>
One approach would be to pass it through a custom defined filter
<%
def myFilter(txt):
return "whatever I want to do it"
>%
${self.body() | myFilter}
精彩评论