Permalink
Browse files

Merge pull request #495 from alekstorm/template_try_else

Add `else` as possible sub-clause in `try` blocks in templates.
  • Loading branch information...
2 parents 983fb8b + 4416c0a commit b875a8b546e42843dabcb0ba96469d959b1ed7e0 @bdarnell bdarnell committed Apr 19, 2012
Showing with 12 additions and 2 deletions.
  1. +2 −2 tornado/template.py
  2. +10 −0 tornado/test/template_test.py
View
4 tornado/template.py
@@ -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 %}``
@@ -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"]),
View
10 tornado/test/template_test.py
@@ -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"))

0 comments on commit b875a8b

Please sign in to comment.