简体   繁体   中英

Jinja2 for Flask not picking up child templates

I am trying to write an email html file with three parts: header, body and footer. These are wrapped inside a primary mail.html file which looks like this:

<html>
   <body>
     <p>
      <table border="0" cellpadding="0" cellspacing="0" style="font-size: 12.727272033691406px; line-height: 1.2em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; border-spacing: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; background-color: rgb(231, 232, 232); border-top-style: solid; border-top-color: rgb(221, 221, 221);" width="100%">
        <tbody>
            <tr>
                <td align="center" style="font-family: arial, sans-serif; margin: 0px;" width="100%">

                    {% block head %}
                    {% endblock %}

                    {% block body %}
                    {% endblock %}
                <br/>

                     {% block footer %}
                     {% endblock %}
                </td>
           </tr>
        </tbody>
     </table>
    </p>
   </body>
</html>

Essentially one parent template with three child temnplates, all in the same directory. Child templates have been described between the following two tags:

{% extends "mail.html" %}
{% block head %}
...
{% endblock %}

When I run the following command:

from jinja2 import Environment, PackageLoader

env = Environment(loader = PackageLoader('mailwrapper','mail_templates'))


template = env.get_template('mail.html')
template.render()

I only get the output with the html of mail.html but the child blocks are ignored and only empty lines are printed out instead. What am I doing wrong here?

To get content into head , body or footer blocks, you should render either head.html template or body.html or footer.html . When you execute head.html template, it takes mail.html template and replaces the blocks with content. On the other hand, when you render mail.html template, the template doesn't know about head.html (or other) template that should replace the blocks with content.

Consider following example.

mail.html

<html>
   <body>
    {% block head %}
    {% endblock %}
   </body>
</html>

head.html:

{% extends "mail.html" %}
{% block head %}
Hello Email Head
{% endblock %}

mailwrapper.py:

from jinja2 import Environment, PackageLoader
env = Environment(loader = PackageLoader('mailwrapper','mail_templates'))

template = env.get_template('head.html')
print(template.render())

>>> <html>
>>>   <body>
>>>     Hello Email Head
>>>   </body>
>>> </html>

template = env.get_template('mail.html')
print(template.render())

>>> <html>
>>>   <body>
>>>     
>>>   </body>
>>> </html>

I would recommend to have 1 main mail.html template with 3 blocks and bunch of other templates that extend the mail.html template, replacing content in all 3 blocks. For example

friendly_mail.html:

{% extends "mail.html" %}
{% block head %}
  Dear ...,
{% endblock %}

{% block body %}
  You have a good day.
{% endblock %}

{% block footer %}
  Sincerely,
    ...
{% endblock %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM