Lightweight template template for python

What is the simplest and easiest html template in Python that I can use to create custom email newsletters.

+3
source share
5 answers

For a really minor template task, Python itself is not so 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 easy templating. It is about as easy.

If you want a little deeper, Jinja2 is the most โ€œdesign friendlyโ€ (read: simple) template engine, according to many.

. , ( , , ).

+11

- 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)
+12
+1

Python Google Titen, 5.5 kB. Titen , str.format .

Mako claims to be light but relatively thick (> 200 kB) compared to Titen. Jinja2 and Django templates also have over 100 kB.

0
source

Try 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
<:#}#:>
-2
source

Source: https://habr.com/ru/post/1779543/


All Articles