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
|
# http://jinja.pocoo.org/docs/2.10/intro/#basic-api-usage
|
||||||
|
|
||||||
# htmlfragment = """<span id='{{id}}'>
|
from jinja2 import Template
|
||||||
# {{ content }}
|
htmlfragment = """<h1 id='{{id}}'>
|
||||||
# </span>"""
|
{{ content }}
|
||||||
# template = Template(htmlfragment)
|
</h1>
|
||||||
# htmlrendered = template.render(content="Jinja What??",
|
"""
|
||||||
# id="iamid")
|
template = Template(htmlfragment)
|
||||||
|
title = template.render(content="XPuB tries JINJA (What??)",
|
||||||
|
id="elementid")
|
||||||
# print(htmlrendered)
|
# 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>
|
users_html = Template('''<ul>
|
||||||
{% for user in users %}
|
{% for user in users %}
|
||||||
<li class="user" id="user_{{ user }}">
|
<li class="user" id="user_{{ user|lower }}">
|
||||||
<i>{{ user }}</i>
|
{% if user[0] == "A" %}
|
||||||
|
<i>{{ user[0] }}</i>{{ user[1:] }}
|
||||||
|
{% else %}
|
||||||
|
<b>{{ user[0] }}</b>{{ user[1:] }}
|
||||||
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</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_1 = users_html.render(users=xpub1)
|
||||||
users_2 = users_html.render(users=xpub2)
|
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
|
# http://jinja.pocoo.org/docs/2.10/templates/#template-inheritance
|
||||||
from jinja2 import FileSystemLoader
|
from jinja2 import FileSystemLoader
|
||||||
from jinja2.environment import Environment
|
from jinja2.environment import Environment
|
||||||
|
|
||||||
env = Environment()
|
env = Environment() # loads templates from the file system
|
||||||
env.loader = FileSystemLoader('.')
|
env.loader = FileSystemLoader('.') # Loads templates from the current directory
|
||||||
tmpl = env.get_template('child.html')
|
tmpl = env.get_template('child.html') # get the template # which itself inherit base.html template
|
||||||
tmpl_render= tmpl.render(mytitle="xpUB",
|
tmpl_render = tmpl.render(mytitle="xpUB",
|
||||||
content=users_1 + users_2)
|
content=title + users_1 + users_2) # render the template
|
||||||
print(tmpl_render)
|
print(tmpl_render)
|
||||||
|
|
||||||
# save
|
# save
|
||||||
with open("index.html","w") as index:
|
with open("index.html", "w") as index:
|
||||||
index.write(tmpl_render)
|
index.write(tmpl_render)
|
||||||
|
|
||||||
|
|
||||||
# contro structures
|
|
||||||
# http://jinja.pocoo.org/docs/2.10/templates/#list-of-control-structures
|
|
||||||
|
|
||||||
|
|
||||||
# print(users_html.render(users=xpub))
|
|
||||||
|
@ -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