From 3e508b3b6a89a262518a677afa987756b6985d4d Mon Sep 17 00:00:00 2001 From: Felinx Lee Date: Tue, 19 Jun 2012 11:25:51 +0800 Subject: [PATCH] Add continue and break statement support in while and for loop for template --- tornado/template.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tornado/template.py b/tornado/template.py index 7072760be..64d8391ea 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -676,7 +676,7 @@ def _format_code(code): return "".join([format % (i + 1, line) for (i, line) in enumerate(lines)]) -def _parse(reader, template, in_block=None): +def _parse(reader, template, in_block=None, in_loop=None): body = _ChunkList([]) while True: # Find next template directive @@ -815,7 +815,11 @@ def _parse(reader, template, in_block=None): elif operator in ("apply", "block", "try", "if", "for", "while"): # parse inner body recursively - block_body = _parse(reader, template, operator) + if operator in ("for", "while"): + block_body = _parse(reader, template, operator, operator) + else: + block_body = _parse(reader, template, operator, in_loop) + if operator == "apply": if not suffix: raise ParseError("apply missing method name on line %d" % line) @@ -829,5 +833,11 @@ def _parse(reader, template, in_block=None): body.chunks.append(block) continue + elif operator in ("break", "continue"): + if not in_loop: + raise ParseError("%s outside %s block" % (operator, set(["for", "while"]))) + body.chunks.append(_Statement(contents, line)) + continue + else: raise ParseError("unknown operator: %r" % operator) -- 2.47.2