python 的轻量级模板引擎

发布于 2024-10-07 09:44:27 字数 55 浏览 0 评论 0原文

这是 Python 中最简单、轻量级的 html 模板引擎,我可以用它来生成定制的电子邮件通讯。

Which is the simplest and light weight html templating engine in Python which I can use to generate customized email newsletters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半暖夏伤 2024-10-14 09:44:27

string.Template 有什么问题吗?这是标准 Python 发行版,并由 PEP 292 涵盖:

from string import Template

form=Template('''Dear $john,

I am sorry to imform you, $john, but you will not be my husband
when you return from the $theater war. So sorry about that. Your
$action has caused me to reconsider.

Yours [NOT!!] forever,

Becky

''')

first={'john':'Joe','theater':'Afgan','action':'love'}
second={'john':'Robert','theater':'Iraq','action':'kiss'}
third={'john':'Jose','theater':'Korean','action':'discussion'}

print form.substitute(first)
print form.substitute(second)
print form.substitute(third)

Anything wrong with string.Template? This is in the standard Python distribution and covered by PEP 292:

from string import Template

form=Template('''Dear $john,

I am sorry to imform you, $john, but you will not be my husband
when you return from the $theater war. So sorry about that. Your
$action has caused me to reconsider.

Yours [NOT!!] forever,

Becky

''')

first={'john':'Joe','theater':'Afgan','action':'love'}
second={'john':'Robert','theater':'Iraq','action':'kiss'}
third={'john':'Jose','theater':'Korean','action':'discussion'}

print form.substitute(first)
print form.substitute(second)
print form.substitute(third)
若水微香 2024-10-14 09:44:27

对于一个非常小的模板任务,Python 本身并没有那么糟糕。示例:

def dynamic_text(name, food):
    return """
    Dear %(name)s,
    We're glad to hear that you like %(food)s and we'll be sending you some more soon.
    """ % {'name':name, 'food':food}

从这个意义上说,您可以在 Python 中使用字符串格式来进行简单的模板化。这就是它所能达到的最轻量。

如果你想更深入一点,Jinja2 是最“设计者友好”(读作:简单)的模板引擎在许多人看来。

您还可以查看 Mako 和 Genshi。最终,选择权在您(具有您想要的功能并与您的系统完美集成)。

For a really minor templating task, Python itself isn't that bad. Example:

def dynamic_text(name, food):
    return """
    Dear %(name)s,
    We're glad to hear that you like %(food)s and we'll be sending you some more soon.
    """ % {'name':name, 'food':food}

In this sense, you can use string formatting in Python for simple templating. That's about as lightweight as it gets.

If you want to go a bit deeper, Jinja2 is the most "designer friendly" (read: simple) templating engine in the opinion of many.

You can also look into Mako and Genshi. Ultimately, the choice is yours (what has the features you'd like and integrates nicely with your system).

放血 2024-10-14 09:44:27

在Google中搜索tiny template Python发现了Titen,其源代码仅为5.5 kB。 Titen 可以对列表进行迭代,而内置的 str.format 则不能。

Mako 声称是轻量级的,但与 Titen 相比,它相对较胖(>200 kB)。 Jinja2 和 Django 模板也远超过 100 kB。

Searching for tiny template Python in Google turned up Titen, whose source code is only 5.5 kB. Titen can do iteration over lists, which the built-in str.format can't.

Mako claims to be lightweight, but it's relatively fat (>200 kB) compared to Titen. Jinja2 and Django Templates are also well over 100 kB.

戈亓 2024-10-14 09:44:27

尝试一下 python-micro-template:

https://github.com/diyism/python-micro -template

使用示例(kivy):

import python_micro_template
...
kvml=open('example_kivy_scrollview.kvml', 'r').read()
kvml=python_micro_template.tpl.parse(kvml)
grid=Builder.load_string(kvml)
...

模板示例(kvml):

<:for i in range(30):#{#:>
Button:
    text: '<:=i:><:for j in range(6):#{#:><:=j:><:#}#:>'
    size: 480, 40
    size_hint: None, None
<:#}#:>

Give a try to python-micro-template:

https://github.com/diyism/python-micro-template

Usage example(kivy):

import python_micro_template
...
kvml=open('example_kivy_scrollview.kvml', 'r').read()
kvml=python_micro_template.tpl.parse(kvml)
grid=Builder.load_string(kvml)
...

Template example(kvml):

<:for i in range(30):#{#:>
Button:
    text: '<:=i:><:for j in range(6):#{#:><:=j:><:#}#:>'
    size: 480, 40
    size_hint: None, None
<:#}#:>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文