官术网_书友最值得收藏!

Templates

Sending back JSON or YAML documents is easy enough, since we're just serializing data. And most microservices produce machine-parseable data. But in some cases, we might need to create documents with some layout--whether it's an HTML page, or a PDF report, or an email.

For anything that's text-based, Flask integrates a template engine called Jinja (http://jinja.pocoo.org). The main reason Flask incorporates Jinja is to produce HTML documents, so you will find helpers like render_template, which generate responses by picking a Jinja template, and provide the output given some data.

But Jinja is not unique to HTML or other tag-based documents. It can create any document as long as it's text-based.

For example, if your microservice sends emails, instead of relying on the standard library's email package to produce the email content, which can be cumbersome, you could use Jinja.

The following is an example of an email template:

Date: {{date}} 
From: {{from}}
Subject: {{subject}}
To: {{to}}
Content-Type: text/plain


Hello {{name}},

We have received your payment!

Below is the list of items we will deliver for lunch:

{% for item in items %}- {{item['name']}} ({{item['price']}} Euros)
{% endfor %}

Thank you for your business!

--
Tarek's Burger

Jinja uses double brackets for marking variables that will be replaced by a value. Variables can be anything that's passed to Jinja at execution time.

You can also use Python's if and for blocks directly in your templates with the {% for x in y % }... {% endfor %} and {% if x %}...{% endif %} notations.

The following is a Python script that uses the email template to produce an entirely valid RFC 822 message, which you can send via SMTP:

    from datetime import datetime 
from jinja2 import Template
from email.utils import format_datetime

def render_email(**data):
with open('email_template.eml') as f:
template = Template(f.read())
return template.render(**data)

data = {'date': format_datetime(datetime.now()),
'to': 'bob@example.com',
'from': 'tarek@ziade.org',
'subject': "Your Tarek's Burger order",
'name': 'Bob',
'items': [{'name': 'Cheeseburger', 'price': 4.5},
{'name': 'Fries', 'price': 2.},
{'name': 'Root Beer', 'price': 3.}]}

print(render_email(**data))

The render_email function uses the Template class to generate the email using the provided data.

Jinja is quite powerful, and comes with many features we won't describe here, since it's out of the chapter's scope. But if you need to do some templating work in your microservices, it's a good choice, and it's present in Flask. Check out http://jinja.pocoo.org/docs for a full documentation on Jinja features.
主站蜘蛛池模板: 东光县| 新乡县| 姚安县| 凤城市| 佳木斯市| 荥阳市| 张家界市| 北海市| 民勤县| 化德县| 施秉县| 高青县| 温宿县| 阜阳市| 两当县| 文水县| 渝中区| 济南市| 英吉沙县| 南阳市| 邯郸县| 定安县| 芜湖县| 秭归县| 油尖旺区| 瓦房店市| 松溪县| 谷城县| 乳山市| 唐河县| 丹凤县| 门头沟区| 陈巴尔虎旗| 丰顺县| 雷山县| 广安市| 仙桃市| 壤塘县| 开阳县| 师宗县| 荥阳市|