]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Refactor template namespace from render_string to its own method.
authorBen Darnell <ben@bendarnell.com>
Mon, 27 Aug 2012 00:43:38 +0000 (17:43 -0700)
committerBen Darnell <ben@bendarnell.com>
Mon, 27 Aug 2012 00:43:38 +0000 (17:43 -0700)
This method may be overridden as an alternative to overriding render_string
and adding to kwargs, or may be used directly to render templates
without going through render_string (as when templates are constructed
directly; see https://github.com/facebook/tornado/issues/564)

tornado/web.py
website/sphinx/web.rst

index 323436f8b8cc6e30253998a423508282e33df1d7..f7eda1f581dfedd1afd279950e0fa250f14f8a49 100644 (file)
@@ -106,7 +106,7 @@ class RequestHandler(object):
 
     def __init__(self, application, request, **kwargs):
         super(RequestHandler, self).__init__()
-        
+
         self.application = application
         self.request = request
         self._headers_written = False
@@ -602,7 +602,20 @@ class RequestHandler(object):
             else:
                 loader = RequestHandler._template_loaders[template_path]
         t = loader.load(template_name)
-        args = dict(
+        namespace = self.get_template_namespace()
+        namespace.update(kwargs)
+        return t.generate(**namespace)
+
+    def get_template_namespace(self):
+        """Returns a dictionary to be used as the default template namespace.
+
+        May be overridden by subclasses to add or modify values.
+
+        The results of this method will be combined with additional
+        defaults in the `tornado.template` module and keyword arguments
+        to `render` or `render_string`.
+        """
+        namespace = dict(
             handler=self,
             request=self.request,
             current_user=self.current_user,
@@ -612,9 +625,8 @@ class RequestHandler(object):
             xsrf_form_html=self.xsrf_form_html,
             reverse_url=self.reverse_url
         )
-        args.update(self.ui)
-        args.update(kwargs)
-        return t.generate(**args)
+        namespace.update(self.ui)
+        return namespace
 
     def create_template_loader(self, template_path):
         settings = self.application.settings
index 55d69a2a603671ac281c1b9d3dfb47535ca06db4..e8c1cd33e259a428fbf5ce0c82e4de6c9775263b 100644 (file)
@@ -9,7 +9,7 @@
 
    Entry points
    ^^^^^^^^^^^^
-   
+
    .. automethod:: RequestHandler.initialize
    .. automethod:: RequestHandler.prepare
    .. automethod:: RequestHandler.on_finish
@@ -47,6 +47,7 @@
    .. automethod:: RequestHandler.finish
    .. automethod:: RequestHandler.render
    .. automethod:: RequestHandler.render_string
+   .. automethod:: RequestHandler.get_template_namespace
    .. automethod:: RequestHandler.redirect
    .. automethod:: RequestHandler.send_error
    .. automethod:: RequestHandler.write_error