如何在Python程序中使用Jinja2模板?

时间:2021-11-12 02:37:35

I have one python script which I want to execute as a daily cron job to send emails to all users. Currently I have hard-coded the html inside the script and it looks dirty. I have read the docs, but I haven't figured out how can I render the template within my script.

我有一个python脚本,我想以cron作业的形式执行它,向所有用户发送电子邮件。目前我已经在脚本中硬编码了html,它看起来很脏。我已经阅读了文档,但还没有找到如何在脚本中呈现模板。

Is there any way that I can have the separate html file with placeholders which I can use python to populate and then send as the email's body?

有什么方法可以让我用占位符来分隔html文件,我可以用python来填充,然后作为邮件的主体发送?

I want something like this:

我想要这样的东西:

mydict = {}
template = '/templates/email.j2'
fillTemplate(mydict)
html = getHtml(filledTemplate)

3 个解决方案

#1


3  

I am going to expand on @Mauro's answer. You would move all of the email HTML and/or text into a template file(s). Then use the Jinja API to read the template in from the file; finally, you would render the template by providing the variables that are in the template.

我将详述@Mauro的答案。您可以将所有的电子邮件HTML和/或文本移动到一个模板文件中。然后使用Jinja API从文件中读取模板;最后,您将通过提供模板中的变量来呈现模板。

# copied directly from the docs
from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader('yourapplication', 'templates'))
template = env.get_template('mytemplate.html')
print template.render(the='variables', go='here')

This is a link to an example of using the API with the template.

这是一个与模板一起使用API的示例的链接。

#2


7  

I was looking to do the same thing using Jinja within the Flask framework. Here's an example borrowed from Miguel Grinberg's Flask tutorial:

我想在Flask框架中使用Jinja做同样的事情。这是一个从Miguel Grinberg的烧瓶教程中借用的例子:

from flask import render_template
from flask.ext.mail import Message
from app import mail

subject = 'Test Email'
sender = 'alice@example.com'
recipients = ['bob@example.com']

msg = Message(subject, sender=sender, recipients=recipients)
msg.body = render_template('emails/test.txt', name='Bob') 
msg.html = render_template('emails/test.html', name='Bob') 
mail.send(msg)

It assumes something like the following templates:

它假定如下模板:

templates/emails/test.txt

模板/电子邮件/用法

Hi {{ name }},
This is just a test.

templates/emails/test.html

模板/电子邮件/ test.html

<p>Hi {{ name }},</p>
<p>This is just a test.</p>

#3


0  

You can use Jinja2, a template language for Python. It has template inheritance feature. Look at this example from official docs.

您可以使用Jinja2,这是Python的模板语言。它具有模板继承特性。看看这个来自官方文档的例子。

#1


3  

I am going to expand on @Mauro's answer. You would move all of the email HTML and/or text into a template file(s). Then use the Jinja API to read the template in from the file; finally, you would render the template by providing the variables that are in the template.

我将详述@Mauro的答案。您可以将所有的电子邮件HTML和/或文本移动到一个模板文件中。然后使用Jinja API从文件中读取模板;最后,您将通过提供模板中的变量来呈现模板。

# copied directly from the docs
from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader('yourapplication', 'templates'))
template = env.get_template('mytemplate.html')
print template.render(the='variables', go='here')

This is a link to an example of using the API with the template.

这是一个与模板一起使用API的示例的链接。

#2


7  

I was looking to do the same thing using Jinja within the Flask framework. Here's an example borrowed from Miguel Grinberg's Flask tutorial:

我想在Flask框架中使用Jinja做同样的事情。这是一个从Miguel Grinberg的烧瓶教程中借用的例子:

from flask import render_template
from flask.ext.mail import Message
from app import mail

subject = 'Test Email'
sender = 'alice@example.com'
recipients = ['bob@example.com']

msg = Message(subject, sender=sender, recipients=recipients)
msg.body = render_template('emails/test.txt', name='Bob') 
msg.html = render_template('emails/test.html', name='Bob') 
mail.send(msg)

It assumes something like the following templates:

它假定如下模板:

templates/emails/test.txt

模板/电子邮件/用法

Hi {{ name }},
This is just a test.

templates/emails/test.html

模板/电子邮件/ test.html

<p>Hi {{ name }},</p>
<p>This is just a test.</p>

#3


0  

You can use Jinja2, a template language for Python. It has template inheritance feature. Look at this example from official docs.

您可以使用Jinja2,这是Python的模板语言。它具有模板继承特性。看看这个来自官方文档的例子。