diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2008-04-13 13:17:21 +0200 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2008-04-13 13:17:21 +0200 |
commit | 2e7b54f7d89d5d3436ce573d1ade8cca26b9f162 (patch) | |
tree | e6476ded9bc2b88c9dfe96b80ffec2721a6fd3c8 /examples | |
parent | 3d8b784a7b443659574bbd35af385ab6ae4b5189 (diff) | |
download | jinja-2e7b54f7d89d5d3436ce573d1ade8cca26b9f162.tar.gz |
moved example tests into a folder
--HG--
branch : trunk
rename : bench.py => examples/bench.py
rename : test.py => examples/test.py
rename : test_filter_and_linestatements.py => examples/test_filter_and_linestatements.py
rename : test_loop_filter.py => examples/test_loop_filter.py
Diffstat (limited to 'examples')
-rw-r--r-- | examples/bench.py | 127 | ||||
-rw-r--r-- | examples/test.py | 27 | ||||
-rw-r--r-- | examples/test_filter_and_linestatements.py | 25 | ||||
-rw-r--r-- | examples/test_loop_filter.py | 12 |
4 files changed, 191 insertions, 0 deletions
diff --git a/examples/bench.py b/examples/bench.py new file mode 100644 index 00000000..0e6b3072 --- /dev/null +++ b/examples/bench.py @@ -0,0 +1,127 @@ +from django.conf import settings +settings.configure() +from django.template import Template as DjangoTemplate, Context as DjangoContext +from jinja2 import Environment as JinjaEnvironment +from mako.template import Template as MakoTemplate +from timeit import Timer + + +jinja_template = JinjaEnvironment( + line_statement_prefix='%', + variable_start_string="${", + variable_end_string="}" +).from_string("""\ +<!doctype html> +<html> + <head> + <title>${page_title|e} + </head> + <body> + <div class="header"> + <h1>${page_title|e}</h1> + </div> + <ul class="navigation"> + % for href, caption in [ + ('index.html', 'Index'), + ('downloads.html', 'Downloads'), + ('products.html', 'Products') + ] + <li><a href="${href|e}">${caption|e}</a></li> + % endfor + </ul> + <div class="table"> + <table> + % for row in table + <tr> + % for cell in row + <td>${cell}</td> + % endfor + </tr> + % endfor + </table> + </div> + </body> +</html>\ +""") + +django_template = DjangoTemplate("""\ +<!doctype html> +<html> + <head> + <title>{{ page_title }} + </head> + <body> + <div class="header"> + <h1>{{ page_title }}</h1> + </div> + <ul class="navigation"> + {% for href, caption in navigation %} + <li><a href="{{ href }}">{{ caption }}</a></li> + {% endfor %} + </ul> + <div class="table"> + <table> + {% for row in table %} + <tr> + {% for cell in row %} + <td>{{ cell }}</td> + {% endfor %} + </tr> + {% endfor %} + </table> + </div> + </body> +</html>\ +""") + +mako_template = MakoTemplate("""\ +<!doctype html> +<html> + <head> + <title>${page_title|h} + </head> + <body> + <div class="header"> + <h1>${page_title|h}</h1> + </div> + <ul class="navigation"> + % for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]: + <li><a href="${href|h}">${caption|h}</a></li> + % endfor + </ul> + <div class="table"> + <table> + % for row in table: + <tr> + % for cell in row: + <td>${cell}</td> + % endfor + </tr> + % endfor + </table> + </div> + </body> +</html>\ +""") + +context = { + 'page_title': 'mitsuhiko\'s benchmark', + '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)] +} + +def test_jinja(): + jinja_template.render(context) + +def test_django(): + c = DjangoContext(context) + c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')] + django_template.render(c) + +def test_mako(): + mako_template.render(**context) + + +for test in 'jinja', 'mako', 'django': + t = Timer(setup='from __main__ import test_%s as bench' % test, + stmt='bench()') + print '%-20s%.4fms' % (test, t.timeit(number=20) / 20) diff --git a/examples/test.py b/examples/test.py new file mode 100644 index 00000000..b62c84fb --- /dev/null +++ b/examples/test.py @@ -0,0 +1,27 @@ +from jinja2 import Environment +from jinja2.loaders import DictLoader + +env = Environment(loader=DictLoader({ +'child.html': u'''\ +{% extends master_layout or 'master.html' %} +{% include helpers = 'helpers.html' %} +{% macro get_the_answer() %}42{% endmacro %} +{% title = 'Hello World' %} +{% block body %} + {{ get_the_answer() }} + {{ helpers.conspirate() }} +{% endblock %} +''', +'master.html': u'''\ +<!doctype html> +<title>{{ title }}</title> +{% block body %}{% endblock %} +''', +'helpers.html': u'''\ +{% macro conspirate() %}23{% endmacro %} +''' +})) + + +tmpl = env.get_template("child.html") +print tmpl.render() diff --git a/examples/test_filter_and_linestatements.py b/examples/test_filter_and_linestatements.py new file mode 100644 index 00000000..c9e8f956 --- /dev/null +++ b/examples/test_filter_and_linestatements.py @@ -0,0 +1,25 @@ +from jinja2 import Environment + + +env = Environment(line_statement_prefix='%', variable_start_string="${", variable_end_string="}") +tmpl = env.from_string("""\ +% macro foo() + ${caller(42)} +% endmacro +<ul> +% for item in seq + <li>${item}</li> +% endfor +</ul> +% call(var) foo() + [${var}] +% endcall +% filter escape + <hello world> + % for item in [1, 2, 3] + - ${item} + % endfor +% endfilter +""") + +print tmpl.render(seq=range(10)) diff --git a/examples/test_loop_filter.py b/examples/test_loop_filter.py new file mode 100644 index 00000000..64f32d35 --- /dev/null +++ b/examples/test_loop_filter.py @@ -0,0 +1,12 @@ +from jinja2 import Environment + +tmpl = Environment().from_string("""\ +<ul> +{% for item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if item % 2 == 0 %} + <li>{{ loop.index }} / {{ loop.length }}: {{ item }}</li> +{% endfor %} +</ul> +{{ 1 if foo else 0 }} +""") + +print tmpl.render(foo=True) |