Armin Ronacher | 649c7b9 | 2007-05-30 20:59:06 +0200 | [diff] [blame] | 1 | import jdebug |
Christoph Hack | e9e43bb | 2008-04-13 23:35:48 +0200 | [diff] [blame] | 2 | from jinja2 import Environment, DictLoader |
Armin Ronacher | 649c7b9 | 2007-05-30 20:59:06 +0200 | [diff] [blame] | 3 | |
| 4 | base_tmpl = """ |
| 5 | {% block content %}Default{% endblock %} |
| 6 | """ |
| 7 | |
| 8 | ### condition is inside of block |
| 9 | |
| 10 | test1 = """ |
| 11 | {% extends 'base' %} |
| 12 | |
| 13 | {% block content %} |
| 14 | {% if False %} |
| 15 | {{ throw_exception() }} |
| 16 | {% endif %} |
| 17 | {% endblock %} |
| 18 | """ |
| 19 | |
| 20 | ### block is inside of condition |
| 21 | |
| 22 | test2 = """ |
| 23 | {% extends 'base' %} |
| 24 | |
| 25 | {% if False %} |
| 26 | {% block content %} |
| 27 | {{ throw_exception() }} |
| 28 | {% endblock %} |
| 29 | {% endif %} |
| 30 | """ |
| 31 | |
| 32 | class TestException(Exception): |
| 33 | pass |
| 34 | |
| 35 | def throw_exception(): |
| 36 | raise TestException() |
| 37 | |
| 38 | env = Environment( |
| 39 | loader=DictLoader(dict(base=base_tmpl)) |
| 40 | ) |
| 41 | |
| 42 | if __name__ == '__main__': |
| 43 | for name in 'test1', 'test2': |
| 44 | template_body = globals().get(name) |
| 45 | template = env.from_string(template_body) |
| 46 | try: |
| 47 | print 'Rendering template:\n"""%s"""' % template_body |
| 48 | template.render(throw_exception=throw_exception) |
| 49 | except TestException: |
| 50 | print 'Result: throw_exception() was called' |
| 51 | else: |
| 52 | print 'Result: throw_exception() was not called' |
| 53 | print |
| 54 | |
| 55 | print 'First template illustrates that condition is working well' |
| 56 | print 'The question is - why {% block %} is being evalueted '\ |
| 57 | 'in false condition in second template?' |