introduction to jinja files
parent
afca511958
commit
9573bb4984
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<link rel="stylesheet" href="stylesheet.css" />
|
||||
<title>{% block title %}{% endblock %}</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="content">
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,9 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{{ mytitle }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="box">
|
||||
{{ content }}
|
||||
</div>
|
||||
{% endblock %}
|
@ -0,0 +1,52 @@
|
||||
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))
|
@ -0,0 +1,68 @@
|
||||
<!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>
|
@ -0,0 +1,7 @@
|
||||
.box{ background: black;
|
||||
color:white;
|
||||
}
|
||||
|
||||
.user{color:yellow;
|
||||
text-decoration: underline;
|
||||
}
|
Loading…
Reference in New Issue