Refactor template namespace from render_string to its own method.
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)
This commit is contained in:
parent
30bc2332fb
commit
58671703d3
|
@ -106,7 +106,7 @@ class RequestHandler(object):
|
||||||
|
|
||||||
def __init__(self, application, request, **kwargs):
|
def __init__(self, application, request, **kwargs):
|
||||||
super(RequestHandler, self).__init__()
|
super(RequestHandler, self).__init__()
|
||||||
|
|
||||||
self.application = application
|
self.application = application
|
||||||
self.request = request
|
self.request = request
|
||||||
self._headers_written = False
|
self._headers_written = False
|
||||||
|
@ -602,7 +602,20 @@ class RequestHandler(object):
|
||||||
else:
|
else:
|
||||||
loader = RequestHandler._template_loaders[template_path]
|
loader = RequestHandler._template_loaders[template_path]
|
||||||
t = loader.load(template_name)
|
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,
|
handler=self,
|
||||||
request=self.request,
|
request=self.request,
|
||||||
current_user=self.current_user,
|
current_user=self.current_user,
|
||||||
|
@ -612,9 +625,8 @@ class RequestHandler(object):
|
||||||
xsrf_form_html=self.xsrf_form_html,
|
xsrf_form_html=self.xsrf_form_html,
|
||||||
reverse_url=self.reverse_url
|
reverse_url=self.reverse_url
|
||||||
)
|
)
|
||||||
args.update(self.ui)
|
namespace.update(self.ui)
|
||||||
args.update(kwargs)
|
return namespace
|
||||||
return t.generate(**args)
|
|
||||||
|
|
||||||
def create_template_loader(self, template_path):
|
def create_template_loader(self, template_path):
|
||||||
settings = self.application.settings
|
settings = self.application.settings
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
Entry points
|
Entry points
|
||||||
^^^^^^^^^^^^
|
^^^^^^^^^^^^
|
||||||
|
|
||||||
.. automethod:: RequestHandler.initialize
|
.. automethod:: RequestHandler.initialize
|
||||||
.. automethod:: RequestHandler.prepare
|
.. automethod:: RequestHandler.prepare
|
||||||
.. automethod:: RequestHandler.on_finish
|
.. automethod:: RequestHandler.on_finish
|
||||||
|
@ -47,6 +47,7 @@
|
||||||
.. automethod:: RequestHandler.finish
|
.. automethod:: RequestHandler.finish
|
||||||
.. automethod:: RequestHandler.render
|
.. automethod:: RequestHandler.render
|
||||||
.. automethod:: RequestHandler.render_string
|
.. automethod:: RequestHandler.render_string
|
||||||
|
.. automethod:: RequestHandler.get_template_namespace
|
||||||
.. automethod:: RequestHandler.redirect
|
.. automethod:: RequestHandler.redirect
|
||||||
.. automethod:: RequestHandler.send_error
|
.. automethod:: RequestHandler.send_error
|
||||||
.. automethod:: RequestHandler.write_error
|
.. automethod:: RequestHandler.write_error
|
||||||
|
|
Loading…
Reference in New Issue