Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added Jinja2 templates

  • Loading branch information...
commit faa2f76b4bd80014e1925f3e8af5d08906d69cb9 1 parent e1af994
@anandology authored
View
1  2010/03/genshi-bench/Readme.md
@@ -5,3 +5,4 @@ Source: http://svn.edgewall.org/repos/genshi/trunk/examples/bench
## Changes
* Added web.py templates
+* Added Jinja2 templates
View
19 2010/03/genshi-bench/basic.py
@@ -184,6 +184,25 @@ def f():
return f
+def jinja2(dirname, verbose=False):
+ from jinja2 import Environment, FileSystemLoader
+ env = Environment(loader=FileSystemLoader([dirname]), autoescape=True)
+
+ t = env.get_template('template.html')
+ env.get_template('header.html')
+ env.get_template('footer.html')
+
+ def f():
+ return t.render(
+ title='Just a test',
+ user='joe',
+ items=['Number %d' % num for num in range(1, 15)]
+ )
+
+ if verbose:
+ print f()
+ return f
+
def run(engines, number=2000, verbose=False):
basepath = os.path.abspath(os.path.dirname(__file__))
for engine in engines:
View
24 2010/03/genshi-bench/bigtable.py
@@ -50,6 +50,11 @@
except ImportError:
WebPyTemplate = None
+try:
+ from jinja2 import Environment as Jinja2Environment
+except ImportError:
+ Jinja2Template = None
+
table = [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10)
for x in range(1000)]
@@ -116,6 +121,23 @@ def test_webpy():
d = webpy_tmpl(table)
unicode(d)
+if Jinja2Environment:
+ env = Jinja2Environment(autoescape=True)
+ jinja2_tmpl = env.from_string("""
+<table>
+ {% for row in table %}
+ <tr>
+ {% for col in row.values() %}
+ <td>{{ col }}</td>
+ {% endfor %}
+ </tr>
+ {% endfor %}
+</table>
+""")
+ def test_jinja2():
+ """jinja2 template"""
+ return jinja2_tmpl.render({"table": table})
+
def test_genshi():
"""Genshi template"""
stream = genshi_tmpl.generate(table=table)
@@ -216,7 +238,7 @@ def test_clearsilver():
def run(which=None, number=10):
tests = ['test_builder', 'test_genshi', 'test_genshi_text',
'test_genshi_builder', 'test_mako', 'test_kid', 'test_kid_et',
- 'test_et', 'test_cet', 'test_clearsilver', 'test_django', 'test_webpy']
+ 'test_et', 'test_cet', 'test_clearsilver', 'test_django', 'test_webpy', 'test_jinja2']
if which:
tests = filter(lambda n: n[5:] in which, tests)
View
2  2010/03/genshi-bench/jinja2/footer.html
@@ -0,0 +1,2 @@
+<div id="footer">
+</div>
View
5 2010/03/genshi-bench/jinja2/header.html
@@ -0,0 +1,5 @@
+<div id="header">
+ <h1>{{ title }}</h1>
+</div>
+
+
View
30 2010/03/genshi-bench/jinja2/template.html
@@ -0,0 +1,30 @@
+<!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 items %}
+ <ul>
+ {% for item in items %}
+ <li{{ loop.last and ' class="last"' | safe or "" }}>$item</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+
+ {% include 'footer.html' %}
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.