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