1. Atsushi Odagiri
  2. mako-inheritance

Commits

Mike Bayer  committed c0ac68f Merge

Merged in fero14041/mako-benchmark-add_jinja (pull request #1)

  • Participants
  • Parent commits 2d31dd9, 395338f
  • Branches default

Comments (0)

Files changed (6)

File examples/bench/basic.py

View file
     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']
+
+# Templates content and constants
+TITLE = 'Just a test'
+USER = 'joe'
+ITEMS = ['Number %d' % num for num in range(1, 15)]
+U_ITEMS = [u(item) for item in ITEMS]
 
 def genshi(dirname, verbose=False):
     from genshi.template import TemplateLoader
     loader = TemplateLoader([dirname], auto_reload=False)
     template = loader.load('template.html')
     def render():
-        data = dict(title='Just a test', user='joe',
-                    items=['Number %d' % num for num in range(1, 15)])
+        data = dict(title=TITLE, user=USER, items=ITEMS)
         return template.generate(**data).render('xhtml')
 
     if verbose:
     from myghty import interp
     interpreter = interp.Interpreter(component_root=dirname)
     def render():
-        data = dict(title='Just a test', user='joe',
-                    items=['Number %d' % num for num in range(1, 15)])
+        data = dict(title=TITLE, user=USER, items=ITEMS)
         buffer = StringIO()
         interpreter.execute("template.myt", request_args=data, out_buffer=buffer)
         return buffer.getvalue()
     lookup = TemplateLookup(directories=[dirname], filesystem_checks=False, disable_unicode=disable_unicode)
     template = lookup.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)])
+        return template.render(title=TITLE, user=USER, list_items=U_ITEMS)
     if verbose:
         print(template.code + " " + render())
     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=TITLE, user=USER, list_items=U_ITEMS)
+    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')
     template = Template(file=filename)
     def render():
-        template.__dict__.update({'title': 'Just a test', 'user': 'joe',
-                                  'list_items': [u('Number %d') % num for num in range(1, 15)]})
+        template.__dict__.update({'title': TITLE, 'user': USER,
+                                  'list_items': U_ITEMS})
         return template.respond()
 
     if verbose:
     tmpl = loader.get_template('template.html')
 
     def render():
-        data = {'title': 'Just a test', 'user': 'joe',
-                'items': ['Number %d' % num for num in range(1, 15)]}
+        data = {'title': TITLE, 'user': USER, 'items': ITEMS}
         return tmpl.render(template.Context(data))
 
     if verbose:
     template = kid.Template(file='template.kid')
     def render():
         template = kid.Template(file='template.kid',
-                                title='Just a test', user='joe',
-                                items=['Number %d' % num for num in range(1, 15)])
+                                title=TITLE, user=USER, items=ITEMS)
         return template.serialize(output='xhtml')
 
     if verbose:
         t = timeit.Timer(setup='from __main__ import %s; render = %s(r"%s", %s)'
                                        % (engine, engine, dirname, verbose),
                                  stmt='render()')
- 
+
         time = t.timeit(number=number) / number
         if verbose:
             print('--------------------------------------------------------')

File examples/bench/jinja2/footer.html

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

File examples/bench/jinja2/header.html

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

File examples/bench/jinja2/template.html

View file
+<!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>

File examples/bench/jinja2_inheritance/base.html

View file
+<!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>

File examples/bench/jinja2_inheritance/template.html

View file
+{% 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 %}