summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Férotin <vincent.ferotin@gmail.com>2012-02-21 21:38:32 +0100
committerVincent Férotin <vincent.ferotin@gmail.com>2012-02-21 21:38:32 +0100
commit583c8e3683fd73eb6d0a6902a4cfb121227905f3 (patch)
treee79383ba125b596af80a7cd5cf48a3bca1e364ec
parent9eae3bc66bf8128b38d2376429f0d3dd12586c81 (diff)
downloadmako-583c8e3683fd73eb6d0a6902a4cfb121227905f3.tar.gz
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.
-rw-r--r--examples/bench/basic.py14
-rw-r--r--examples/bench/jinja2/footer.html2
-rw-r--r--examples/bench/jinja2/header.html3
-rw-r--r--examples/bench/jinja2/template.html31
-rw-r--r--examples/bench/jinja2_inheritance/base.html24
-rw-r--r--examples/bench/jinja2_inheritance/template.html15
6 files changed, 88 insertions, 1 deletions
diff --git a/examples/bench/basic.py b/examples/bench/basic.py
index 381840c..85ffae5 100644
--- a/examples/bench/basic.py
+++ b/examples/bench/basic.py
@@ -43,7 +43,8 @@ def u(stringlit):
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
@@ -84,6 +85,17 @@ def mako(dirname, verbose=False):
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')
diff --git a/examples/bench/jinja2/footer.html b/examples/bench/jinja2/footer.html
new file mode 100644
index 0000000..1b00330
--- /dev/null
+++ b/examples/bench/jinja2/footer.html
@@ -0,0 +1,2 @@
+<div id="footer">
+</div>
diff --git a/examples/bench/jinja2/header.html b/examples/bench/jinja2/header.html
new file mode 100644
index 0000000..ccb2fb7
--- /dev/null
+++ b/examples/bench/jinja2/header.html
@@ -0,0 +1,3 @@
+<div id="header">
+ <h1>{{ title }}</h1>
+</div>
diff --git a/examples/bench/jinja2/template.html b/examples/bench/jinja2/template.html
new file mode 100644
index 0000000..5965e0d
--- /dev/null
+++ b/examples/bench/jinja2/template.html
@@ -0,0 +1,31 @@
+<!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>
diff --git a/examples/bench/jinja2_inheritance/base.html b/examples/bench/jinja2_inheritance/base.html
new file mode 100644
index 0000000..c10a434
--- /dev/null
+++ b/examples/bench/jinja2_inheritance/base.html
@@ -0,0 +1,24 @@
+<!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>
diff --git a/examples/bench/jinja2_inheritance/template.html b/examples/bench/jinja2_inheritance/template.html
new file mode 100644
index 0000000..7e1b781
--- /dev/null
+++ b/examples/bench/jinja2_inheritance/template.html
@@ -0,0 +1,15 @@
+{% 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 %}