Commits

Anonymous committed bbb5d24

Add Jinja2 example to benchmark:

* add two new templates directory: ``examples/bench/jinja2`` and ``examples/bench/jinja2_inheritance``;
* add two set of Jinja2 templates, matching those related to Mako;
* add new ``jinja2`` function in ``examples/bench/basic.py`` module to load these new templates.

Comments (0)

Files changed (6)

examples/bench/basic.py

     else:
         return stringlit.decode('latin1')
 
-__all__ = ['mako', 'mako_inheritance', 'cheetah', 'django', 'myghty', 'genshi', 'kid']
+__all__ = ['mako', 'mako_inheritance', 'jinja2', 'jinja2_inheritance',
+            'cheetah', 'django', 'myghty', 'genshi', 'kid']
 
 def genshi(dirname, verbose=False):
     from genshi.template import TemplateLoader
     return render
 mako_inheritance = mako
 
+def jinja2(dirname, verbose=False):
+    from jinja2 import Environment, FileSystemLoader
+    env = Environment(loader=FileSystemLoader(dirname))
+    template = env.get_template('template.html')
+    def render():
+        return template.render(title="Just a test", user="joe", list_items=[u('Number %d') % num for num in range(1,15)])
+    if verbose:
+        print(render())
+    return render
+jinja2_inheritance = jinja2
+
 def cheetah(dirname, verbose=False):
     from Cheetah.Template import Template
     filename = os.path.join(dirname, 'template.tmpl')

examples/bench/jinja2/footer.html

+<div id="footer">
+</div>

examples/bench/jinja2/header.html

+<div id="header">
+  <h1>{{ title }}</h1>
+</div>

examples/bench/jinja2/template.html

+<!DOCTYPE html
+    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
+  <head>
+    <title>{{ title }}</title>
+  </head>
+  <body>
+
+    {%- macro greeting(name) %}
+      <p>hello {{ name }}</p>
+    {%- endmacro %}
+
+    {% include "header.html" %}
+
+    {{ greeting(user) }}
+    {{ greeting('me') }}
+    {{ greeting('world') }}
+
+    <h2>Loop</h2>
+    {%- if list_items %}
+      <ul>
+        {%- for list_item in list_items %}
+	<li {{ "class='last'" if loop.last else "" }}>{{ list_item }}</li>
+        {%- endfor %}
+      </ul>
+    {%- endif %}
+
+    {% include "footer.html" %}
+  </body>
+</html>

examples/bench/jinja2_inheritance/base.html

+<!DOCTYPE html
+    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
+  <head>
+    <title>{{ title }}</title>
+  </head>
+  <body>
+
+{%- macro greeting(name) %}
+      <p>hello {{ name }}</p>
+{%- endmacro %}
+
+    <div id="header">
+      <h1>{{ title }}</h1>
+    </div>
+
+{%- block body %}{%- endblock %}
+
+    <div id="footer">
+    </div>
+
+  </body>
+</html>

examples/bench/jinja2_inheritance/template.html

+{% extends "base.html" %}
+{% block body %}
+    {{ greeting(user) }}
+    {{ greeting('me') }}
+    {{ greeting('world') }}
+
+    <h2>Loop</h2>
+    {%- if list_items %}
+      <ul>
+        {%- for list_item in list_items %}
+	<li {{ "class='last'" if loop.last else ""}}>{{ list_item }}</li>
+        {%- endfor %}
+      </ul>
+    {%- endif %}
+{% endblock body %}