``{% while *condition* %}... {% end %}``
Same as the python ``while`` statement. ``{% break %}`` and
``{% continue %}`` may be used inside the loop.
+
+``{% whitespace *mode* %}``
+ Sets the whitespace mode for the remainder of the current file
+ (or until the next ``{% whitespace %}`` directive). See
+ `filter_whitespace` for available options. New in Tornado 4.3.
"""
from __future__ import absolute_import, division, print_function, with_statement
return body
elif operator in ("extends", "include", "set", "import", "from",
- "comment", "autoescape", "raw", "module"):
+ "comment", "autoescape", "whitespace", "raw",
+ "module"):
if operator == "comment":
continue
if operator == "extends":
fn = None
template.autoescape = fn
continue
+ elif operator == "whitespace":
+ mode = suffix.strip()
+ # Validate the selected mode
+ filter_whitespace(mode, '')
+ reader.whitespace = mode
+ continue
elif operator == "raw":
block = _Expression(suffix, line, raw=True)
elif operator == "module":
self.assertEqual(loader.load("foo.html").generate(), b" foo ")
self.assertEqual(loader.load("bar.txt").generate(), b" bar ")
+ def test_whitespace_directive(self):
+ loader = DictLoader({
+ "foo.html": """\
+{% whitespace oneline %}
+ {% for i in range(3) %}
+ {{ i }}
+ {% end %}
+{% whitespace all %}
+ pre\tformatted
+"""})
+ self.assertEqual(loader.load("foo.html").generate(),
+ b" 0 1 2 \n pre\tformatted\n")
+
class TemplateLoaderTest(unittest.TestCase):
def setUp(self):