Skip to content

Commit

Permalink
Merge pull request tornadoweb#495 from alekstorm/template_try_else
Browse files Browse the repository at this point in the history
Add `else` as possible sub-clause in `try` blocks in templates.
  • Loading branch information
bdarnell committed Apr 19, 2012
2 parents 983fb8b + 4416c0a commit b875a8b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tornado/template.py
Expand Up @@ -165,7 +165,7 @@ def add(x, y):
``{% set *x* = *y* %}``
Sets a local variable.
``{% try %}...{% except %}...{% finally %}...{% end %}``
``{% try %}...{% except %}...{% finally %}...{% else %}...{% end %}``
Same as the python ``try`` statement.
``{% while *condition* %}... {% end %}``
Expand Down Expand Up @@ -757,7 +757,7 @@ def _parse(reader, template, in_block=None):

# Intermediate ("else", "elif", etc) blocks
intermediate_blocks = {
"else": set(["if", "for", "while"]),
"else": set(["if", "for", "while", "try"]),
"elif": set(["if"]),
"except": set(["try"]),
"finally": set(["try"]),
Expand Down
10 changes: 10 additions & 0 deletions tornado/test/template_test.py
Expand Up @@ -101,6 +101,16 @@ def test_if(self):
self.assertEqual(template.generate(x=5), b("yes"))
self.assertEqual(template.generate(x=3), b("no"))

def test_try(self):
template = Template(utf8("""{% try %}
try{% set y = 1/x %}
{% except %}-except
{% else %}-else
{% finally %}-finally
{% end %}"""))
self.assertEqual(template.generate(x=1), b("\ntry\n-else\n-finally\n"))
self.assertEqual(template.generate(x=0), b("\ntry-except\n-finally\n"))

def test_comment_directive(self):
template = Template(utf8("{% comment blah blah %}foo"))
self.assertEqual(template.generate(), b("foo"))
Expand Down

0 comments on commit b875a8b

Please sign in to comment.