From 77d25461fafc4f0506efd6666dc9a03fd3ac8977 Mon Sep 17 00:00:00 2001 From: Castro0o Date: Mon, 4 Mar 2019 21:18:51 +0100 Subject: [PATCH] fixes and comments --- Jinja-prototype/base.html | 2 +- Jinja-prototype/createhtml.py | 60 ++++++++++++++++--------------- Jinja-prototype/index.html | 68 ----------------------------------- 3 files changed, 32 insertions(+), 98 deletions(-) delete mode 100644 Jinja-prototype/index.html diff --git a/Jinja-prototype/base.html b/Jinja-prototype/base.html index 801553b..15ac928 100644 --- a/Jinja-prototype/base.html +++ b/Jinja-prototype/base.html @@ -1,7 +1,7 @@ - + {% block title %}{% endblock %} diff --git a/Jinja-prototype/createhtml.py b/Jinja-prototype/createhtml.py index 74034a4..1e5fbb1 100644 --- a/Jinja-prototype/createhtml.py +++ b/Jinja-prototype/createhtml.py @@ -1,52 +1,54 @@ -from jinja2 import Template - -# render template +# RENDER TEMPLATE: # http://jinja.pocoo.org/docs/2.10/intro/#basic-api-usage -# htmlfragment = """ -# {{ content }} -# """ -# template = Template(htmlfragment) -# htmlrendered = template.render(content="Jinja What??", -# id="iamid") +from jinja2 import Template +htmlfragment = """

+ {{ content }} +

+ """ +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(''' ''') +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)) \ No newline at end of file +with open("index.html", "w") as index: + index.write(tmpl_render) diff --git a/Jinja-prototype/index.html b/Jinja-prototype/index.html deleted file mode 100644 index 964bb6f..0000000 --- a/Jinja-prototype/index.html +++ /dev/null @@ -1,68 +0,0 @@ - - - - - xpUB - - -
- -
-
    - -
  • - Pedro -
  • - -
  • - Rita -
  • - -
  • - Simon -
  • - -
  • - Artemis -
  • - -
  • - Bo -
  • - -
  • - Biyi -
  • - -
    - -
  • - Tash -
  • - -
  • - Angeliki -
  • - -
  • - Alice -
  • - -
  • - Alex -
  • - -
  • - Joca -
  • - -
  • - Zalan -
  • - -
-
- -
- - \ No newline at end of file