You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
Python
52 lines
1.2 KiB
Python
6 years ago
|
from jinja2 import Template
|
||
|
|
||
|
|
||
|
# render template
|
||
|
# http://jinja.pocoo.org/docs/2.10/intro/#basic-api-usage
|
||
|
|
||
|
# htmlfragment = """<span id='{{id}}'>
|
||
|
# {{ content }}
|
||
|
# </span>"""
|
||
|
# template = Template(htmlfragment)
|
||
|
# htmlrendered = template.render(content="Jinja What??",
|
||
|
# id="iamid")
|
||
|
# print(htmlrendered)
|
||
|
|
||
|
xpub1 = ["Pedro","Rita","Simon","Artemis","Bo","Biyi"]
|
||
|
xpub2 = ["Tash", "Angeliki","Alice","Alex", "Joca", "Zalan"]
|
||
|
users_html = Template('''<ul>
|
||
|
{% for user in users %}
|
||
|
<li class="user" id="user_{{ user }}">
|
||
|
<i>{{ user }}</i>
|
||
|
</li>
|
||
|
{% endfor %}
|
||
|
</ul>
|
||
|
''')
|
||
|
|
||
|
users_1 = users_html.render(users=xpub1)
|
||
|
users_2 = users_html.render(users=xpub2)
|
||
|
print(users_1, users_2)
|
||
|
|
||
|
|
||
|
# template-inheritance
|
||
|
# http://jinja.pocoo.org/docs/2.10/templates/#template-inheritance
|
||
|
from jinja2 import FileSystemLoader
|
||
|
from jinja2.environment import Environment
|
||
|
|
||
|
env = Environment()
|
||
|
env.loader = FileSystemLoader('.')
|
||
|
tmpl = env.get_template('child.html')
|
||
|
tmpl_render= tmpl.render(mytitle="xpUB",
|
||
|
content=users_1 + users_2)
|
||
|
print(tmpl_render)
|
||
|
|
||
|
# save
|
||
|
with open("index.html","w") as index:
|
||
|
index.write(tmpl_render)
|
||
|
|
||
|
|
||
|
# contro structures
|
||
|
# http://jinja.pocoo.org/docs/2.10/templates/#list-of-control-structures
|
||
|
|
||
|
|
||
|
# print(users_html.render(users=xpub))
|