diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/rwbench/django/_form.html | 1 | ||||
-rw-r--r-- | examples/rwbench/django/_input_field.html | 1 | ||||
-rw-r--r-- | examples/rwbench/django/_textarea.html | 1 | ||||
-rw-r--r-- | examples/rwbench/django/index.html | 29 | ||||
-rw-r--r-- | examples/rwbench/django/layout.html | 29 | ||||
-rw-r--r-- | examples/rwbench/djangoext.py | 128 | ||||
-rw-r--r-- | examples/rwbench/jinja/index.html | 5 | ||||
-rw-r--r-- | examples/rwbench/mako/index.html | 4 | ||||
-rw-r--r-- | examples/rwbench/rwbench.py | 22 |
9 files changed, 211 insertions, 9 deletions
diff --git a/examples/rwbench/django/_form.html b/examples/rwbench/django/_form.html new file mode 100644 index 0000000..9c4f710 --- /dev/null +++ b/examples/rwbench/django/_form.html @@ -0,0 +1 @@ +<form action="{{ action }}" method="{{ method }}">{{ body }}</form> diff --git a/examples/rwbench/django/_input_field.html b/examples/rwbench/django/_input_field.html new file mode 100644 index 0000000..290fdbd --- /dev/null +++ b/examples/rwbench/django/_input_field.html @@ -0,0 +1 @@ +<input type="{{ type }}" value="{{ value }}" name="{{ name }}"> diff --git a/examples/rwbench/django/_textarea.html b/examples/rwbench/django/_textarea.html new file mode 100644 index 0000000..7f10424 --- /dev/null +++ b/examples/rwbench/django/_textarea.html @@ -0,0 +1 @@ +<textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ value }}</textarea> diff --git a/examples/rwbench/django/index.html b/examples/rwbench/django/index.html new file mode 100644 index 0000000..b5da4cc --- /dev/null +++ b/examples/rwbench/django/index.html @@ -0,0 +1,29 @@ +{% extends "layout.html" %} +{% block page_title %}Index Page{% endblock %} +{% block body %} + {% for article in articles %} + {% if article.published %} + <div class="article"> + <h2><a href="{{ article.href }}">{{ article.title }}</a></h2> + <p class="meta">written by <a href="{{ article.user.href }}">{{ article.user.username }}</a> on {{ article.pub_date|dateformat }}</p> + <div class="text">{{ article.body|safe }}</div> + </div> + {% endif %} + {% endfor %} + {% form %} + <dl> + <dt>Name</dt> + <dd>{% input_field 'name' %}</dd> + <dt>E-Mail</dt> + <dd>{% input_field 'email' %}</dd> + <dt>URL</dt> + <dd>{% input_field 'url' %}</dd> + <dt>Comment</dd> + <dd>{% textarea 'comment' %}</dd> + <dt>Captcha</dt> + <dd>{% input_field 'captcha' %}</dd> + </dl> + {% input_field '' 'submit' 'Submit' %} + {% input_field 'cancel' 'submit' 'Cancel' %} + {% endform %} +{% endblock %} diff --git a/examples/rwbench/django/layout.html b/examples/rwbench/django/layout.html new file mode 100644 index 0000000..60039ce --- /dev/null +++ b/examples/rwbench/django/layout.html @@ -0,0 +1,29 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> +<html> +<head> + <title>{% block page_title %}{% endblock %} | RealWorld Benchmark</title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> +</head> +<body> + <div class="contents"> + <div class="header"> + <h1>RealWorld Benchmark</h1> + <blockquote><p> + A less stupid benchmark for Mako and Jinja2 to get an impression how + code changes affect runtime performance. + </p></blockquote> + </div> + <ul class="navigation"> + {% for href, caption in page_navigation %} + <li><a href="{{ href }}">{{ caption }}</a></li> + {% endfor %} + </ul> + <div class="body"> + {% block body %}{% endblock %} + </div> + <div class="footer"> + © Copyright 2008 by I don't know who. + </div> + </div> +</body> +</html> diff --git a/examples/rwbench/djangoext.py b/examples/rwbench/djangoext.py new file mode 100644 index 0000000..7cc0971 --- /dev/null +++ b/examples/rwbench/djangoext.py @@ -0,0 +1,128 @@ +# -*- coding: utf-8 -*- +from rwbench import ROOT +from os.path import join +from django.conf import settings +settings.configure(TEMPLATE_DIRS=(join(ROOT, 'django'),)) +from django.template import loader as django_loader, Context as DjangoContext, \ + Node, NodeList, Variable, TokenParser +from django import template as django_template_module +from django.template import Library + + +# for django extensions. We monkey patch our extensions in so that +# we don't have to initialize a more complex django setup. +django_extensions = django_template_module.Library() +django_template_module.builtins.append(django_extensions) + + +from rwbench import dateformat +django_extensions.filter(dateformat) + + +def var_or_none(x): + if x is not None: + return Variable(x) + + +# and more django extensions +@django_extensions.tag +def input_field(parser, token): + p = TokenParser(token.contents) + args = [p.value()] + while p.more(): + args.append(p.value()) + return InputFieldNode(*args) + + +@django_extensions.tag +def textarea(parser, token): + p = TokenParser(token.contents) + args = [p.value()] + while p.more(): + args.append(p.value()) + return TextareaNode(*args) + + +@django_extensions.tag +def form(parser, token): + p = TokenParser(token.contents) + args = [] + while p.more(): + args.append(p.value()) + body = parser.parse(('endform',)) + parser.delete_first_token() + return FormNode(body, *args) + + +class InputFieldNode(Node): + + def __init__(self, name, type=None, value=None): + self.name = var_or_none(name) + self.type = var_or_none(type) + self.value = var_or_none(value) + + def render(self, context): + name = self.name.resolve(context) + type = 'text' + value = '' + if self.type is not None: + type = self.type.resolve(context) + if self.value is not None: + value = self.value.resolve(context) + tmpl = django_loader.get_template('_input_field.html') + return tmpl.render(DjangoContext({ + 'name': name, + 'type': type, + 'value': value + })) + + +class TextareaNode(Node): + + def __init__(self, name, rows=None, cols=None, value=None): + self.name = var_or_none(name) + self.rows = var_or_none(rows) + self.cols = var_or_none(cols) + self.value = var_or_none(value) + + def render(self, context): + name = self.name.resolve(context) + rows = 10 + cols = 40 + value = '' + if self.rows is not None: + rows = int(self.rows.resolve(context)) + if self.cols is not None: + cols = int(self.cols.resolve(context)) + if self.value is not None: + value = self.value.resolve(context) + tmpl = django_loader.get_template('_textarea.html') + return tmpl.render(DjangoContext({ + 'name': name, + 'rows': rows, + 'cols': cols, + 'value': value + })) + + +class FormNode(Node): + + def __init__(self, body, action=None, method=None): + self.body = body + self.action = action + self.method = method + + def render(self, context): + body = self.body.render(context) + action = '' + method = 'post' + if self.action is not None: + action = self.action.resolve(context) + if self.method is not None: + method = self.method.resolve(context) + tmpl = django_loader.get_template('_form.html') + return tmpl.render(DjangoContext({ + 'body': body, + 'action': action, + 'method': method + })) diff --git a/examples/rwbench/jinja/index.html b/examples/rwbench/jinja/index.html index 2b97e70..8338113 100644 --- a/examples/rwbench/jinja/index.html +++ b/examples/rwbench/jinja/index.html @@ -2,7 +2,7 @@ {% from "helpers.html" import input_field, textarea, form %} {% block page_title %}Index Page{% endblock %} {% block body %} - {%- for article in articles %} + {%- for article in articles if article.published %} <div class="article"> <h2><a href="{{ article.href|e }}">{{ article.title|e }}</a></h2> <p class="meta">written by <a href="{{ article.user.href|e @@ -20,7 +20,10 @@ <dd>{{ input_field('url') }}</dd> <dt>Comment</dd> <dd>{{ textarea('comment') }}</dd> + <dt>Captcha</dt> + <dd>{{ input_field('captcha') }}</dd> </dl> {{ input_field(type='submit', value='Submit') }} + {{ input_field('cancel', type='submit', value='Cancel') }} {%- endcall %} {% endblock %} diff --git a/examples/rwbench/mako/index.html b/examples/rwbench/mako/index.html index 33bfe32..dde6c8e 100644 --- a/examples/rwbench/mako/index.html +++ b/examples/rwbench/mako/index.html @@ -5,6 +5,7 @@ <%namespace file="helpers.html" import="input_field, textarea, form" /> <%def name="page_title()">Index Page</%def> % for article in articles: + <% if not article.published: continue %> <div class="article"> <h2><a href="${article.href|h}">${article.title|h}</a></h2> <p class="meta">written by <a href="${article.user.href|h @@ -22,6 +23,9 @@ <dd>${input_field('url')}</dd> <dt>Comment</dd> <dd>${textarea('comment')}</dd> + <dt>Captcha</dt> + <dd>${input_field('captcha')}</dd> </dl> ${input_field(type='submit', value='Submit')} + ${input_field(name='cancel', type='submit', value='Cancel')} </%call> diff --git a/examples/rwbench/rwbench.py b/examples/rwbench/rwbench.py index 1f3e387..4714da4 100644 --- a/examples/rwbench/rwbench.py +++ b/examples/rwbench/rwbench.py @@ -12,6 +12,8 @@ """ import sys from os.path import join, dirname, abspath +ROOT = abspath(dirname(__file__)) + from random import choice, randrange from datetime import datetime from timeit import Timer @@ -20,19 +22,14 @@ from jinja2.utils import generate_lorem_ipsum from mako.lookup import TemplateLookup -ROOT = abspath(dirname(__file__)) - - def dateformat(x): return x.strftime('%Y-%m-%d') jinja_env = Environment(loader=FileSystemLoader(join(ROOT, 'jinja'))) jinja_env.filters['dateformat'] = dateformat - mako_lookup = TemplateLookup(directories=[join(ROOT, 'mako')]) - class Article(object): def __init__(self, id): @@ -42,6 +39,7 @@ class Article(object): self.user = choice(users) self.body = generate_lorem_ipsum() self.pub_date = datetime.utcfromtimestamp(randrange(10 ** 9, 2 * 10 ** 9)) + self.published = True class User(object): @@ -57,8 +55,10 @@ navigation = [ ('index', 'Index'), ('about', 'About'), ('foo?bar=1', 'Foo with Bar'), - ('foo?bar=2&s=x', 'Foo with X') -] + ('foo?bar=2&s=x', 'Foo with X'), + ('blah', 'Blub Blah'), + ('hehe', 'Haha'), +] * 5 context = dict(users=users, articles=articles, page_navigation=navigation) @@ -74,9 +74,15 @@ def test_mako(): mako_template.render_unicode(**context) +from djangoext import django_loader, DjangoContext +django_template = django_loader.get_template('index.html') +def test_django(): + django_template.render(DjangoContext(context)) + + if __name__ == '__main__': sys.stdout.write('Realworldish Benchmark:\n') - for test in 'jinja', 'mako': + for test in 'jinja', 'mako', 'django': t = Timer(setup='from __main__ import test_%s as bench' % test, stmt='bench()') sys.stdout.write(' >> %-20s<running>' % test) |