Search code examples
pythonflaskmako

Access <%def>s in included mako file


I am currently implementing a feature in a Flask application. I have multiple parent mako files which includes the same child mako file.

# parent.mako
...
<%def name="title()">${page_title}</%def>
<%include file="child.mako"/>
...


# child.mako
<h1>${title()} Discussion</h1>

Basically, I am trying to pass the title() into the child.mako. However, the def block does not seem to render in the included file. Can anyone else give me a solution? I hope it will be simple and easy.


Solution

  • You can try to import namespace into your child template file:

    <%namespace name="title_namespace" file="parent.mako"/>
    

    then you would be able to use it like:

    ${title_namespace.title()}
    

    I hope it will help you.