Tornado – Templates – Run time and Cached

Website development often calls for reuse of pages. For instance, when you open your Citibank account after login the welcome page you see is same what other bank customers see but its customized with your name and settings. Do you think Citi creates so many web pages for all its customers? Well, they use, what’s called as template. Template is a layout or a skeleton that separates content from presentation and helps in mass production of web pages..

Control Statements in Templates

Tornado supports templates with expressions and control statements; straight from Tornadoweb, “Control statements are surronded by {% and %}, e.g., {% if len(items) > 2 %}. Expressions are surrounded by {{ and }}, e.g., {{ items[0] }}.”


<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<table border="1">
{% for key,value in dict.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% end %}
</table>
</body>
</html>

view raw

template.html

hosted with ❤ by GitHub


import tornado.ioloop
import tornado.web
import time
class ItWorks(tornado.web.RequestHandler):
def get(self):
self.write("It Works!!")
class Article(tornado.web.RequestHandler):
def get(self):
articles = {"tornado":"Python",
"phpcake":"PHP"}
self.render('template.html', title='Articles', dict=articles)
## title and dict is handled by template.html
application = tornado.web.Application([
(r"/", ItWorks),
(r"/articles", Article),
],debug=True)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

In the above example, we have created a template (template.html) that requires a scalar value for title and a dictionary for listing dictionary items in the form of a table (List data types also work well in Tornado templates). The python code provide these values in the code line

self.render(‘template.html’, title=’Articles’, dict=articles)

Output

tornado templates example

Run-time Template Generation and Compiling Templates

Let’s now understand two important methods in Tornado templates, how do they work and how are they implemented:

  1. Template()
  2. Loader()

Template() – helps in generating a template at run time. For instance, there is a web page with lesser content and that need not be rendered quite often, we can utilize this Template method.

Loader() – helps in compiling and caching templates from a predefined location of your machine. Compiling and caching improves performance of web server while rendering web pages.

Example 2


<html>{{ name }}</html>

view raw

template.html

hosted with ❤ by GitHub


import tornado.ioloop
import tornado.web
from tornado.template import Template
from tornado.template import Loader
class Article(tornado.web.RequestHandler):
def get(self):
## Works with Template method of tornado.template
t = Template("<html>{{ name }}</html>")
self.write(t.generate(name="John"))
## Works with Loader method of tornado.template
loader = Loader(".")
self.write(loader.load("template.html").generate(name="John"))
application = tornado.web.Application([
(r"/articles", Article),
],debug=True)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

In example 2,

t = Template(“<html>{{ name }}</html>”) — generates template at run time

self.write(loader.load(“template.html”).generate(name=”John”))  — template is compiled, cached and loaded

One thought on “Tornado – Templates – Run time and Cached

Comments are closed.