]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add {# ... #} construct to comment out entire sections 359/head
authorAlek Storm <alek.storm@gmail.com>
Tue, 13 Sep 2011 16:20:19 +0000 (16:20 +0000)
committerAlek Storm <alek.storm@gmail.com>
Tue, 13 Sep 2011 16:20:19 +0000 (16:20 +0000)
tornado/template.py
tornado/test/template_test.py

index f28f40355da164493e7b3341528360da1d496136..fea31c1f7353c4cc398259d29ca0da834be7934d 100644 (file)
@@ -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("}}")
index 0d5fad9ee7cdb074be0bc049db591e1238cf3046..a46f489f80ec57eac22c4a99f94741686fbf1ac1 100644 (file)
@@ -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"))