fixes and comments
parent
9573bb4984
commit
77d25461fa
@ -1,52 +1,54 @@
|
||||
from jinja2 import Template
|
||||
|
||||
|
||||
# render 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")
|
||||
from jinja2 import Template
|
||||
htmlfragment = """<h1 id='{{id}}'>
|
||||
{{ content }}
|
||||
</h1>
|
||||
"""
|
||||
template = Template(htmlfragment)
|
||||
title = template.render(content="XPuB tries JINJA (What??)",
|
||||
id="elementid")
|
||||
# print(htmlrendered)
|
||||
|
||||
xpub1 = ["Pedro","Rita","Simon","Artemis","Bo","Biyi"]
|
||||
xpub2 = ["Tash", "Angeliki","Alice","Alex", "Joca", "Zalan"]
|
||||
|
||||
# CONTROL STRUCTURES: loops and if conditions
|
||||
# http://jinja.pocoo.org/docs/2.10/templates/#list-of-control-structures
|
||||
users_html = Template('''<ul>
|
||||
{% for user in users %}
|
||||
<li class="user" id="user_{{ user }}">
|
||||
<i>{{ user }}</i>
|
||||
<li class="user" id="user_{{ user|lower }}">
|
||||
{% if user[0] == "A" %}
|
||||
<i>{{ user[0] }}</i>{{ user[1:] }}
|
||||
{% else %}
|
||||
<b>{{ user[0] }}</b>{{ user[1:] }}
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
''')
|
||||
|
||||
xpub1 = ["Pedro", "Rita", "Simon", "Artemis", "Bo", "Biyi", "Tancredi"]
|
||||
xpub2 = ["Tash", "Angeliki", "Alice", "Alex", "Joca", "Zalan"]
|
||||
# render the same template with 2 different lists, a string
|
||||
users_1 = users_html.render(users=xpub1)
|
||||
users_2 = users_html.render(users=xpub2)
|
||||
print(users_1, users_2)
|
||||
users_2 = users_html.render(users="xpub2")
|
||||
# print(users_1, users_2)
|
||||
|
||||
|
||||
# template-inheritance
|
||||
# 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)
|
||||
env = Environment() # loads templates from the file system
|
||||
env.loader = FileSystemLoader('.') # Loads templates from the current directory
|
||||
tmpl = env.get_template('child.html') # get the template # which itself inherit base.html template
|
||||
tmpl_render = tmpl.render(mytitle="xpUB",
|
||||
content=title + users_1 + users_2) # render the template
|
||||
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))
|
||||
with open("index.html", "w") as index:
|
||||
index.write(tmpl_render)
|
||||
|
@ -1,68 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<link rel="stylesheet" href="style.css" />
|
||||
<title>xpUB</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="content">
|
||||
|
||||
<div class="box">
|
||||
<ul>
|
||||
|
||||
<li class="user" id="user_Pedro">
|
||||
<i>Pedro</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Rita">
|
||||
<i>Rita</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Simon">
|
||||
<i>Simon</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Artemis">
|
||||
<i>Artemis</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Bo">
|
||||
<i>Bo</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Biyi">
|
||||
<i>Biyi</i>
|
||||
</li>
|
||||
|
||||
</ul><ul>
|
||||
|
||||
<li class="user" id="user_Tash">
|
||||
<i>Tash</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Angeliki">
|
||||
<i>Angeliki</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Alice">
|
||||
<i>Alice</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Alex">
|
||||
<i>Alex</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Joca">
|
||||
<i>Joca</i>
|
||||
</li>
|
||||
|
||||
<li class="user" id="user_Zalan">
|
||||
<i>Zalan</i>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue