From 363e37392aa779bc306f48aa64d05f001e60c5f5 Mon Sep 17 00:00:00 2001 From: Alek Storm Date: Tue, 13 Sep 2011 16:20:19 +0000 Subject: [PATCH] Add {# ... #} construct to comment out entire sections --- tornado/template.py | 14 +++++++++++++- tornado/test/template_test.py | 7 ++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tornado/template.py b/tornado/template.py index f28f40355..fea31c1f7 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -92,6 +92,9 @@ to the current autoescape setting and inserted into the output. Other template directives use ``{% %}``. These tags may be escaped as ``{{!`` and ``{%!`` if you need to include a literal ``{{`` or ``{%`` in the output. +To comment out a section so that it is omitted from the output, surround it +with ``{# ... #}``. + ``{% apply *function* %}...{% end %}`` Applies a function to the output of all template code between ``apply`` and ``end``:: @@ -637,7 +640,7 @@ def _parse(reader, template, in_block=None): return body # If the first curly brace is not the start of a special token, # start searching from the character after it - if reader[curly + 1] not in ("{", "%"): + if reader[curly + 1] not in ("{", "%", "#"): curly += 1 continue # When there are more than 2 curlies in a row, use the @@ -665,6 +668,15 @@ def _parse(reader, template, in_block=None): body.chunks.append(_Text(start_brace)) continue + # Comment + if start_brace == "{#": + end = reader.find("#}") + if end == -1: + raise ParseError("Missing end expression #} on line %d" % line) + contents = reader.consume(end).strip() + reader.consume(2) + continue + # Expression if start_brace == "{{": end = reader.find("}}") diff --git a/tornado/test/template_test.py b/tornado/test/template_test.py index 0d5fad9ee..a46f489f8 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -18,6 +18,11 @@ class TemplateTest(LogTrapTestCase): template = Template("2 + 2 = {{ 2 + 2 }}") self.assertEqual(template.generate(), b("2 + 2 = 4")) + def test_comment(self): + template = Template("Hello{# TODO i18n #} {{ name }}!") + self.assertEqual(template.generate(name=utf8("Ben")), + b("Hello Ben!")) + def test_include(self): loader = DictLoader({ "index.html": '{% include "header.html" %}\nbody text', @@ -89,7 +94,7 @@ class TemplateTest(LogTrapTestCase): self.assertEqual(template.generate(x=5), b("yes")) self.assertEqual(template.generate(x=3), b("no")) - def test_comment(self): + def test_comment_directive(self): template = Template(utf8("{% comment blah blah %}foo")) self.assertEqual(template.generate(), b("foo")) -- 2.47.2