From the Firehose to the Web Browser
Weâre already collecting the data we need, and Tornado is ready to respond to requests for updates. Basically, all we have left to do is build the web page and write the JavaScript.
Templates in Tornado
The template language in Tornado is based on the templates in Django. Both of these projects contain rich templating languages where standard HTML files get parsed based on logic written in a subset of Python. However, a big difference between the templating language that inspired Tornado and Tornado itself is the ability to include arbitrary Python code. In Django, youâre restricted to variables, filters, and standard flow control statements. To go beyond the most basic logic, you must create new template filters and run those. With Tornado, youâre allowed to use any valid Python expression, including functions and list comprehensions. As long as itâs a valid Python expression, it will work in these templates.
Tornado offers a straightforward mapping of most of Pythonâs
standard flow control statements. You can specify for
loops, if
statements,
try
/catch
blocks, and many of the
basic control statements that come to mind. These control statements are
wrapped between {%
and %}
.
Developers can also specify arbitrary expressions; those are marked up
by wrapping the code with {{
and
}}
. The expression is evaluated and the result is then placed directly into the HTML. The following is an example template that contains both flow control and a ...
Get Building the Realtime User Experience now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.