Skip to content

Commit

Permalink
Add the ability to escape template commands with {{! and {%!
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Jun 8, 2011
1 parent 4264418 commit 02ce53b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tornado/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,15 @@ def _parse(reader, template, in_block=None):
start_brace = reader.consume(2)
line = reader.line

# Template directives may be escaped as "{{!" or "{%!".
# In this case output the braces and consume the "!".
# This is especially useful in conjunction with jquery templates,
# which also use double braces.
if reader.remaining() and reader[0] == "!":
reader.consume(1)
body.chunks.append(_Text(start_brace))
continue

# Expression
if start_brace == "{{":
end = reader.find("}}")
Expand Down
11 changes: 10 additions & 1 deletion tornado/test/template_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tornado.escape import utf8, native_str
from tornado.template import Template, DictLoader
from tornado.template import Template, DictLoader, ParseError
from tornado.testing import LogTrapTestCase
from tornado.util import b, bytes_type

Expand Down Expand Up @@ -50,6 +50,15 @@ def test_relative_load(self):
self.assertEqual(loader.load("a/1.html").generate(),
b("ok"))

def test_escaping(self):
self.assertRaises(ParseError, lambda: Template("{{"))
self.assertRaises(ParseError, lambda: Template("{%"))
self.assertEqual(Template("{{!").generate(), b("{{"))
self.assertEqual(Template("{%!").generate(), b("{%"))
self.assertEqual(Template("{{ 'expr' }} {{!jquery expr}}").generate(),
b("expr {{jquery expr}}"))


class AutoEscapeTest(LogTrapTestCase):
def setUp(self):
self.templates = {
Expand Down

0 comments on commit 02ce53b

Please sign in to comment.