Permalink
Browse files

fix empty if/try... statement

  • Loading branch information...
yqz committed Jun 24, 2012
1 parent d37ac92 commit 9df37f22719c72cccae0f8e8bd0c73815b95a6fd
Showing with 19 additions and 2 deletions.
  1. +14 −2 tornado/template.py
  2. +5 −0 tornado/test/template_test.py
View
@@ -492,8 +492,20 @@ def each_child(self):
def generate(self, writer):
writer.write_line("%s:" % self.statement, self.line)
- with writer.indent():
- self.body.generate(writer)
+ need_content = True
+ for chunk in self.body.chunks:
+ if isinstance(chunk, _IntermediateControlBlock):
+ if need_content:
+ with writer.indent():
+ writer.write_line("pass", self.line)
+ need_content = True
+ else:
+ need_content = False
+ with writer.indent():
+ chunk.generate(writer)
+ if need_content:
+ with writer.indent():
+ writer.write_line("pass", self.line)
class _IntermediateControlBlock(_Node):
@@ -101,6 +101,11 @@ def test_if(self):
self.assertEqual(template.generate(x=5), b("yes"))
self.assertEqual(template.generate(x=3), b("no"))
+ template = Template(utf8("{% if x > 4 %}{%elif x < 0%}{%else%}{%end%}"))
+ self.assertEqual(template.generate(x=5), b(""))
+ self.assertEqual(template.generate(x=3), b(""))
+ self.assertEqual(template.generate(x=-1), b(""))
+
def test_try(self):
template = Template(utf8("""{% try %}
try{% set y = 1/x %}

0 comments on commit 9df37f2

Please sign in to comment.