Hello to all,
While updating my python libraries, I noticed that migrating from Jinja 2.7
to Jinja 2.7.1 results in my templates not generating as they should any
longer. Most probably, this is due to this "bugfix" : "reverted support for
defining blocks in included templates as this broke existing templates for
users".
I choose Jinja 2.7 as a templating library for these reasons:
- - Expressiveness (possibility to put some logic in templates)
- - Speed
- - The same templates can be used for server and client sides
(in Javascript via Nunjucks)
- - Possibility to render partial templates (i.e. only generate
the navbar if it's the only part I want to update in the DOM, both from
server and client side)
Now, preventing included templates from defining new blocks completely
breaks my last point. I have included as an attachment a small example of
what I am doing with the library and the results both with 2.7 and 2.7.1.
As you can read, I used the "bugfixed" feature so that I could write
partial parts of the DOM as complete standalone templates
(head_layout_template, body_layout_template). These standalone templates
are the one which can be rendered by themselves when partially updating the
DOM. However, when needed, these partial templates could be inserted in a
parent template (document_layout_template) by using a glue template
(head_body_layout_template). This mechanism of glue template is what no
longer works: in the example, the {{content}} block is correctly inserted
in Jinja 2.7 but completely ignored without any warning in Jinja 2.7.1
I am not a professional template designer, and maybe I am completely
misusing the library but this mechanism of defining new blocks in included
templates is what convinced me of the technical superiority of Jinja. I
would like to know if there is a simple way to get the same functionality
or to un-bugfix, without modifying the library, or an alternate way to
design my templates so that I can get the same functionality than before
(i.e. write full standalone templates for Ajax which are also used to
generate the full web page) (and while keeping compatibility with Nunjucks)
Thanks in advance for your advice/help!
Jinja 2.7 results:
<!DOCTYPE html><html lang=""><head><meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Test
page</title>
<!-- Le styles --><!-- HTML5 shim, for IE6-8 support of HTML5 elements
--><!-- Fav and touch icons --><!-- CSS Fonts --></head><body><div
id="main">
<!-- Contents should show this line -->
</div>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster
--></body></html>
Jinja 2.7.1 results:
<!DOCTYPE html><html lang=""><head><meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Test
page</title>
<!-- Le styles --><!-- HTML5 shim, for IE6-8 support of HTML5 elements
--><!-- Fav and touch icons --><!-- CSS Fonts --></head><body><!-- Le
javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster
--></body></html>
--
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pocoo-libs.
For more options, visit https://groups.google.com/groups/opt_out.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from jinja2 import Template, DictLoader, Environment
##******************************************************************************
document_layout_template = u"""<!DOCTYPE html><html lang="{{page_language}}">
{%- block head %}{% endblock -%}
{%- block body %}{% endblock -%}
</html>"""
##******************************************************************************
head_layout_template = u"""<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
{%- block metanames -%}{%- endblock -%}
<title>{{title}}</title>
<!-- Le styles -->
{%- block stylesheets %}{% endblock -%}
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
{%- block shims %}{% endblock -%}
<!-- Fav and touch icons -->
{%- block favtouchicons %}{% endblock -%}
<!-- CSS Fonts -->
{%- block cssfonts %}{% endblock -%}
"""
body_layout_template = u"""{%- block header %}{% endblock -%}
{%- block content %}{% endblock -%}
{%- block footer %}{% endblock -%}
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
{%- block javascripts %}{% endblock -%}
"""
head_body_layout_template = u"""{%- extends "document_layout.html" -%}
{%- block head -%}
<head>
{%- include "head_layout.html" -%}
</head>
{%- endblock -%}
{%- block body -%}
<body>
{%- include "body_layout.html" -%}
</body>
{%- endblock -%}
"""
##******************************************************************************
generic_template = u"""
{%- extends "head_body_layout.html" -%}
{%- block content -%}
<div id="main">
<!-- Contents should show this line -->
</div>
{% endblock %}
"""
##******************************************************************************
class jinja_templates:
def __init__(self):
self.d = { "document_layout.html": document_layout_template,
"head_layout.html": head_layout_template,
"body_layout.html": body_layout_template,
"head_body_layout.html": head_body_layout_template,
"generic.html": generic_template,
}
loader = DictLoader(self.d)
env = Environment(loader = loader)
env.globals["title"] = "Test page"
env.globals["language"] = "en"
self.env = env
def render(self, template, *args, **kwargs):
return self.env.get_template(template).render(*args, **kwargs)
##******************************************************************************
class template_holder:
def __init__(self):
self.engine = jinja_templates()
def render(self, *args, **kwargs):
text = self.engine.render("generic.html", **kwargs)
return text
##******************************************************************************
def test():
t = template_holder()
text = t.render("coucou")
print text
if __name__ == "__main__":
test()