How to render an HTML email template with Jinja2?

Question:

I want to dynamically render data in an HTML email template with Jinja2 and Python. I’m new both in Python and Jinja2. This is what I have done so far:

  1. My boilerplate
dynamic
   /dynamic.html
   /script.py
  1. I have my HTML template. Just showing you the extract where I added the for loop from Jinja2:
<table bgcolor="#FEF8D5" cellspacing="0" cellpadding="0" width="100%">
                        <tr>
                            <td align="center" bgcolor="#FEF8D5" cellspacing="0">
                                <h2 style="font-family: Helvetica, sans-serif; font-size: 31px; color: #FF341A;">Honorable Mentions</h2>
                            </td>
                        </tr>
                        <tr align="center" valign="center">
                            {% for movie in data["movies"] %}
                            <p>{{ movie["title"] }}</p>
                            {% endfor %}
                        </tr>
                    </table>
  1. I have a Python script:
from jinja2 import Template, Environment, FileSystemLoader

env = Environment(
    loader=FileSystemLoader('.')
)

template = env.get_template('dynamic.html')

def get_data():
    data = []
    data.append(
        {
         "movies": [
             {         
                 "title": 'Terminator',
                 "description": 'One soldier is sent back to protect her from the killing machine. He must find Sarah before the Terminator can carry out its mission.'
             },
             {                 
                 "title": 'Seven Years in Tibet',
                 "description": 'Seven Years in Tibet is a 1997 American biographical war drama film based on the 1952 book of the same name written by Austrian mountaineer Heinrich Harrer on his experiences in Tibet.'
             },
             {               
                 "title": 'The Lion King',
                 "description": 'A young lion prince is born in Africa, thus making his uncle Scar the second in line to the throne. Scar plots with the hyenas to kill King Mufasa and Prince Simba, thus making himself King. The King is killed and Simba is led to believe by Scar that it was his fault, and so flees the kingdom in shame.'
             }
         ]
         })
    return data

data = get_data()

output = template.render(data=data[0])

print(output)

When I run python script.py in console, it gives me the whole HTML correctly rendered: that means, the <p> elements with the movie titles. This makes me think I don’t have any syntax mistakes. However, when I open the HTML file in the browser, the titles are not rendered and it only shows the Jinja code.

As far as I have read and understood, it is not necessary to use Flask to render Jinja, and since I am not creating a web app but an email I’m not sure if is necessary or appropiate to do so.

I’d appreciate your help.

Asked By: thatmare

||

Answers:

If you’re opening dynamic.html, then, well, that is your template and it’s a good thing it’s showing the Jinja template.

You aren’t writing your output to a file in your example (just printing it to the terminal), so maybe add

with open("output.html") as f:
    f.write(output)

at the end and then open output.html in your browser.

Answered By: AKX