Tornado – 'Global variables' in tornado?
Question:
class MainHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render("index.html", messages=MessageMixin.cache)
So the MainHandler
does not pass request
or current_user
to index.html
. But in index.html
I tried <p>{{ current_user }}</p> <p>{{ request }}</p>
and then there’s a lot of output generated. So is this some kind of ‘global variable’ in Tornado ?
Answers:
Several things are given to you for free in Tornado templates.
These variables do not need to be passed in – this is what you are seeing this with current_user and request.
Here is a list of all the variables you get by default
They are part of the default template context in Tornado. The documentation actually covers all of the available ones
-
The secret is in source code!
-
tornado.web has a function named ‘get_template_namespace’, you even can overwrite
-
code detail:
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,
locale=self.locale,
_=self.locale.translate,
pgettext=self.locale.pgettext,
static_url=self.static_url,
xsrf_form_html=self.xsrf_form_html,
reverse_url=self.reverse_url
)
namespace.update(self.ui)
return namespace
To define custom global variables that I want to be available across the Tornado server/app I just add them to the Application itself while setting up the server:
myapp = tornado.web.Application(... all your settings ... )
myapp.myglobalvar = "somevalue"
Then across your classes you can access it by:
class MyClass(tornado.web.RequestHandler):
def get(self):
print("value", self.application.myglobalvar)
class MainHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render("index.html", messages=MessageMixin.cache)
So the MainHandler
does not pass request
or current_user
to index.html
. But in index.html
I tried <p>{{ current_user }}</p> <p>{{ request }}</p>
and then there’s a lot of output generated. So is this some kind of ‘global variable’ in Tornado ?
Several things are given to you for free in Tornado templates.
These variables do not need to be passed in – this is what you are seeing this with current_user and request.
Here is a list of all the variables you get by default
They are part of the default template context in Tornado. The documentation actually covers all of the available ones
-
The secret is in source code!
-
tornado.web has a function named ‘get_template_namespace’, you even can overwrite
-
code detail:
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,
locale=self.locale,
_=self.locale.translate,
pgettext=self.locale.pgettext,
static_url=self.static_url,
xsrf_form_html=self.xsrf_form_html,
reverse_url=self.reverse_url
)
namespace.update(self.ui)
return namespace
To define custom global variables that I want to be available across the Tornado server/app I just add them to the Application itself while setting up the server:
myapp = tornado.web.Application(... all your settings ... )
myapp.myglobalvar = "somevalue"
Then across your classes you can access it by:
class MyClass(tornado.web.RequestHandler):
def get(self):
print("value", self.application.myglobalvar)