Fix empty statement block #546

Closed
wants to merge 2 commits into from
View
16 tornado/template.py
@@ -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)
+ pass_line_number = self.line
+ for chunk in self.body.chunks:
+ if isinstance(chunk, _IntermediateControlBlock):
+ if pass_line_number:
+ with writer.indent():
+ writer.write_line("pass", pass_line_number)
+ pass_line_number = chunk.line
+ else:
+ pass_line_number = None
+ with writer.indent():
+ chunk.generate(writer)
+ if pass_line_number:
+ with writer.indent():
+ writer.write_line("pass", pass_line_number)
class _IntermediateControlBlock(_Node):
View
5 tornado/test/template_test.py
@@ -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 %}