python的轻量级模板引擎

时间:2022-09-10 20:03:53

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

这是Python中最简单,重量最轻的html模板引擎,我可以使用它来生成自定义的电子邮件新闻稿。

5 个解决方案

#1


11  

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

对于一个非常小的模板任务,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}

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

从这个意义上讲,您可以在Python中使用字符串格式来进行简单的模板化。这就像它的轻量级一样。

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

如果你想更深入一点,Jinja2是许多人认为最“设计友好”(阅读:简单)模板引擎。

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).

你也可以看看Mako和Genshi。最终,您可以选择(具有您喜欢的功能,并与您的系统很好地集成)。

#2


12  

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

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)

#3


1  

I think Werkzeug Mini Templates fit the bill pretty well.

我认为Werkzeug迷你模板非常适合这个法案。

Here's the source code on Github.

这是Github上的源代码。

#4


0  

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.

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

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.

Mako声称自己很轻巧,但与Titen相比,它相对较胖(> 200 kB)。 Jinja2和Django模板也超过100 kB。

#5


-2  

Give a try to python-micro-template:

试试python-micro-template:

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

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

Usage example(kivy):

用法示例(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):

模板示例(kvml):

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

#1


11  

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

对于一个非常小的模板任务,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}

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

从这个意义上讲,您可以在Python中使用字符串格式来进行简单的模板化。这就像它的轻量级一样。

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

如果你想更深入一点,Jinja2是许多人认为最“设计友好”(阅读:简单)模板引擎。

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).

你也可以看看Mako和Genshi。最终,您可以选择(具有您喜欢的功能,并与您的系统很好地集成)。

#2


12  

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

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)

#3


1  

I think Werkzeug Mini Templates fit the bill pretty well.

我认为Werkzeug迷你模板非常适合这个法案。

Here's the source code on Github.

这是Github上的源代码。

#4


0  

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.

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

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.

Mako声称自己很轻巧,但与Titen相比,它相对较胖(> 200 kB)。 Jinja2和Django模板也超过100 kB。

#5


-2  

Give a try to python-micro-template:

试试python-micro-template:

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

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

Usage example(kivy):

用法示例(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):

模板示例(kvml):

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