Skip to content

Commit

Permalink
Introduce {% module %} syntax for calling UIModules.
Browse files Browse the repository at this point in the history
Add linkify and xsrf_form_html as default modules.
  • Loading branch information
bdarnell committed Jun 5, 2011
1 parent 4686042 commit ce3e1de
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion demos/chat/templates/index.html
Expand Up @@ -24,7 +24,7 @@
<td style="padding-left:5px">
<input type="submit" value="{{ _("Post") }}"/>
<input type="hidden" name="next" value="{{ request.path }}"/>
{% raw xsrf_form_html() %}
{% module xsrf_form_html() %}
</td>
</tr>
</table>
Expand Down
2 changes: 1 addition & 1 deletion demos/chat/templates/message.html
@@ -1 +1 @@
<div class="message" id="m{{ message["id"] }}"><b>{{ message["from"] }}: </b>{% raw linkify(message["body"]) %}</div>
<div class="message" id="m{{ message["id"] }}"><b>{{ message["from"] }}: </b>{% module linkify(message["body"]) %}</div>
8 changes: 7 additions & 1 deletion tornado/template.py
Expand Up @@ -388,6 +388,10 @@ def generate(self, writer):
writer.current_template.autoescape)
writer.write_line("_buffer.append(_tmp)")

class _Module(_Expression):
def __init__(self, expression):
super(_Module, self).__init__("modules." + expression,
raw=True)

class _Text(_Node):
def __init__(self, value):
Expand Down Expand Up @@ -588,7 +592,7 @@ def _parse(reader, template, in_block=None):
return body

elif operator in ("extends", "include", "set", "import", "from",
"comment", "autoescape", "raw"):
"comment", "autoescape", "raw", "module"):
if operator == "comment":
continue
if operator == "extends":
Expand Down Expand Up @@ -616,6 +620,8 @@ def _parse(reader, template, in_block=None):
continue
elif operator == "raw":
block = _Expression(suffix, raw=True)
elif operator == "module":
block = _Module(suffix)
body.chunks.append(block)
continue

Expand Down
27 changes: 22 additions & 5 deletions tornado/test/web_test.py
@@ -1,5 +1,6 @@
from tornado.escape import json_decode, utf8, to_unicode, recursive_unicode, native_str
from tornado.iostream import IOStream
from tornado.template import DictLoader
from tornado.testing import LogTrapTestCase, AsyncHTTPTestCase
from tornado.util import b, bytes_type
from tornado.web import RequestHandler, _O, authenticated, Application, asynchronous, url
Expand Down Expand Up @@ -236,13 +237,24 @@ def describe(s):
'query': describe(self.get_argument("foo")),
})

class LinkifyHandler(RequestHandler):
def get(self):
self.render("linkify.html", message="http://example.com")

class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
def get_app(self):
return Application([
url("/typecheck/(.*)", TypeCheckHandler, name='typecheck'),
url("/decode_arg/(.*)", DecodeArgHandler),
url("/decode_arg_kw/(?P<arg>.*)", DecodeArgHandler),
])
loader = DictLoader({
"linkify.html": "{% module linkify(message) %}"
})
urls = [
url("/typecheck/(.*)", TypeCheckHandler, name='typecheck'),
url("/decode_arg/(.*)", DecodeArgHandler),
url("/decode_arg_kw/(?P<arg>.*)", DecodeArgHandler),
url("/linkify", LinkifyHandler),
]
return Application(urls,
template_loader=loader,
autoescape="xhtml_escape")

def test_types(self):
response = self.fetch("/typecheck/asdf?foo=bar",
Expand Down Expand Up @@ -274,3 +286,8 @@ def test_decode_argument(self):
self.assertEqual(data, {u'path': [u'bytes', u'c3a9'],
u'query': [u'bytes', u'c3a9'],
})

def test_uimodule_unescaped(self):
response = self.fetch("/linkify")
self.assertEqual(response.body,
b("<a href=\"http://example.com\">http://example.com</a>"))
11 changes: 10 additions & 1 deletion tornado/web.py
Expand Up @@ -1101,7 +1101,8 @@ def __init__(self, handlers=None, default_host="", transforms=None,
self.named_handlers = {}
self.default_host = default_host
self.settings = settings
self.ui_modules = {}
self.ui_modules = {'linkify': _linkify,
'xsrf_form_html': _xsrf_form_html}
self.ui_methods = {}
self._wsgi = wsgi
self._load_ui_modules(settings.get("ui_modules", {}))
Expand Down Expand Up @@ -1609,6 +1610,14 @@ def html_body(self):
def render_string(self, path, **kwargs):
return self.handler.render_string(path, **kwargs)

class _linkify(UIModule):
def render(self, text, **kwargs):
return escape.linkify(text, **kwargs)

class _xsrf_form_html(UIModule):
def render(self):
return self.handler.xsrf_form_html()

class URLSpec(object):
"""Specifies mappings between URLs and handlers."""
def __init__(self, pattern, handler_class, kwargs={}, name=None):
Expand Down

0 comments on commit ce3e1de

Please sign in to comment.