blob: baaae0d784eb8242f53ccc8adc4207d41f8dd7bf [file] [log] [blame]
Armin Ronacher649c7b92007-05-30 20:59:06 +02001import jdebug
Christoph Hacke9e43bb2008-04-13 23:35:48 +02002from jinja2 import Environment, DictLoader
Armin Ronacher649c7b92007-05-30 20:59:06 +02003
4base_tmpl = """
5{% block content %}Default{% endblock %}
6"""
7
8### condition is inside of block
9
10test1 = """
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
22test2 = """
23{% extends 'base' %}
24
25{% if False %}
26 {% block content %}
27 {{ throw_exception() }}
28 {% endblock %}
29{% endif %}
30"""
31
32class TestException(Exception):
33 pass
34
35def throw_exception():
36 raise TestException()
37
38env = Environment(
39 loader=DictLoader(dict(base=base_tmpl))
40)
41
42if __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?'