Armin Ronacher | 8ff24c4 | 2007-03-21 20:33:45 +0100 | [diff] [blame] | 1 | from jinja import Environment, DictLoader |
| 2 | from jinja.exceptions import TemplateNotFound |
Armin Ronacher | 7977e5c | 2007-03-12 07:22:17 +0100 | [diff] [blame] | 3 | from colubrid.debug import DebuggedApplication |
| 4 | from wsgiref.simple_server import make_server |
Armin Ronacher | 7977e5c | 2007-03-12 07:22:17 +0100 | [diff] [blame] | 5 | |
Armin Ronacher | 8ff24c4 | 2007-03-21 20:33:45 +0100 | [diff] [blame] | 6 | e = Environment(loader=DictLoader({ |
| 7 | '/': u''' |
| 8 | <html> |
| 9 | <head> |
| 10 | <title>Various Broken Templates</title> |
| 11 | <style type="text/css"> |
| 12 | body { |
| 13 | margin: 2em; |
| 14 | font-size: 1.5em; |
| 15 | font-family: sans-serif |
| 16 | } |
| 17 | a { |
| 18 | color: #d00; |
| 19 | } |
| 20 | </style> |
| 21 | </head> |
| 22 | <body> |
| 23 | <h1>Various Broken Templates</h1> |
| 24 | <p> |
| 25 | This small WSGI application serves some Jinja templates that |
| 26 | are just broken. It uses the colubrid traceback middleware to |
| 27 | render those errors including source code. |
| 28 | </p> |
| 29 | <ul> |
| 30 | <li><a href="syntax_error">syntax error</a></li> |
| 31 | <li><a href="runtime_error">runtime error</a></li> |
| 32 | <li><a href="nested_syntax_error">nested syntax error</a></li> |
| 33 | <li><a href="nested_runtime_error">nested runtime error</a></li> |
| 34 | </ul> |
| 35 | </body> |
| 36 | </html> |
| 37 | ''', |
| 38 | '/syntax_error': u''' |
| 39 | {% for item in foo %} |
| 40 | ... |
| 41 | {% endif %} |
| 42 | ''', |
| 43 | '/runtime_error': u''' |
| 44 | {% set foo = 1 / 0 %} |
| 45 | ''', |
| 46 | '/nested_runtime_error': u''' |
| 47 | {% include 'runtime_broken' %} |
| 48 | ''', |
| 49 | '/nested_syntax_error': u''' |
| 50 | {% include 'syntax_broken' %} |
| 51 | ''', |
| 52 | |
| 53 | 'runtime_broken': '''\ |
| 54 | This is an included template |
| 55 | {% set a = 1 / 0 %}''', |
| 56 | 'syntax_broken': '''\ |
| 57 | This is an included template |
| 58 | {% raw %}just some foo''' |
| 59 | })) |
| 60 | |
| 61 | |
| 62 | def test(environ, start_response): |
| 63 | try: |
| 64 | tmpl = e.get_template(environ.get('PATH_INFO') or '/') |
| 65 | except TemplateNotFound: |
| 66 | start_response('404 NOT FOUND', [('Content-Type', 'text/plain')]) |
| 67 | return ['NOT FOUND'] |
| 68 | start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')]) |
| 69 | return [tmpl.render().encode('utf-8')] |
Armin Ronacher | 7977e5c | 2007-03-12 07:22:17 +0100 | [diff] [blame] | 70 | |
Armin Ronacher | 18b3d0b | 2007-03-14 20:41:27 +0100 | [diff] [blame] | 71 | make_server("localhost", 7000, DebuggedApplication(test)).serve_forever() |