Tornado Tip: Variables & Functions in Tornado Templates
Tornado has a very fast and flexible template system that is reminiscent of other template systems, such as that found in web.py. I have found that in practical use of the template system, the documentation is thin in some areas. A good example is in what variables and functions are exposed to the templates by default.
To that end, here is a list of the variables and functions available to a template as defined in both template.py and web.py:- _ (underscore)
An alias for the locale.translate() function. - current_user
The current_user object as returned by RequestHandler.get_current_user(). - datetime
The datetime module. Example: {{ datetime.date.today().year }} returns the current year. - escape
A function that escapes a string so it is valid within XML or XHTML. - handler
The request handler that called the self.render function to process the template. Example: {{handler.static_url(‘foo’)}} will run the static_url function in RequestHandler returning a full URL path, pre-pending static_url. - json_encode
A function that JSON-encodes the given Python object. - locale
The locale value as returned by RequestHandler.get_user_locale(). - request
The request object that is passed into the Request Handler. - reverse_url
Given a full module and class (myapp.Homepage) it will look at the handler to URI mapping and return the URL for the given class. - squeeze
A function that replaces all sequences of whitespace chars with a single space. - static_url
The value of the static_url property passed in to the application settings. - url_escape
A function that returns a valid URL-encoded version of the given value. - xsrf_form_html
If using the cross-site forgery protection, this function returns the hidden input field containing the xrsf variable.
This is the current list as of today's master branch on github. If you're using 0.2 there are a few functions that are not in that version. Did I miss one or get something wrong? Please let me know.