Skip to content

Commit 327099a

Browse files
author
Kenneth Reitz
committed
Merge pull request realpython#295 from tommy3001/master
Tornado web server and Jinja2 templating system added with review
2 parents 979c882 + cee6b23 commit 327099a

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

docs/scenarios/web.rst

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ application that is not commonly found in other web frameworks.
9090
Support can be found on its `mailing list <http://werkzeug.pocoo.org/community/#mailinglist>`_.
9191

9292

93+
Tornado
94+
--------
95+
`Tornado <http://http://www.tornadoweb.org/>`_ is a scalable, non-blocking web server and web application framework with
96+
a relative simple usage. Tornado is known for his high performance.
97+
It was initially developed for `friendfeed <http://friendfeed.com/>`_ , a real time chat and blog system.
98+
99+
In the Jinja2 template engine example it is used to serve the rendered pages.
100+
101+
93102
Pyramid
94103
--------
95104

@@ -269,6 +278,121 @@ and to the templates themselves.
269278
the parts where the HTML template passes some variable content
270279
to the javascript code.
271280

281+
282+
283+
Jinja2
284+
------
285+
`Jinja2 <http://jinja.pocoo.org/>`_ is a template engine which is similar to the Django template system with some extra features. It is a text-based template
286+
language and thus can be used to generate any markup. It allows customization of filters, tags, tests and globals.
287+
Unlike the template system implemented in the Django Framework it allows to call functions. The Code is staying under the BSD license.
288+
289+
Here some important html tags in Jinja2:
290+
291+
.. code-block:: html
292+
293+
{# This is a comment #}
294+
295+
{# The next tag is a variable output: #}
296+
{{title}}
297+
298+
{# Tag for a block, can be replaced through inheritance with other html code #}
299+
{% block head %}
300+
<h1>This is the head!</h1>
301+
{% endblock %}
302+
303+
{# Output of an array as an iteration #}
304+
{% for item in list %}
305+
<li>{{ item }}</li>
306+
{% endfor %}
307+
308+
309+
310+
The next listings is an example of a web site in combination with the tornado web server. Tornado is not very complicate
311+
to use.
312+
313+
.. code-block:: python
314+
315+
# import Jinja2
316+
from jinja2 import Environment, FileSystemLoader
317+
318+
# import Tornado
319+
import tornado.ioloop
320+
import tornado.web
321+
322+
# Load tamplate file templates/site.html
323+
TEMPLATE_FILE = "site.html"
324+
templateLoader = FileSystemLoader( searchpath="templates/" )
325+
templateEnv = Environment( loader=templateLoader )
326+
template = templateEnv.get_template(TEMPLATE_FILE)
327+
328+
# List for famous movie rendering
329+
movie_list = [[1,"The Hitchhiker's Guide to the Galaxy"],[2,"Back to future"],[3,"Matrix"]]
330+
331+
# template.render() returns a string which contains the rendered html
332+
html_output = template.render(list=movie_list,
333+
title="Here is my favorite movie list")
334+
335+
# Handler for main page
336+
class MainHandler(tornado.web.RequestHandler):
337+
def get(self):
338+
# Returns rendered template string to the browser request
339+
self.write(html_output)
340+
341+
# Assign handler to the server root (127.0.0.1:PORT/)
342+
application = tornado.web.Application([
343+
(r"/", MainHandler),
344+
])
345+
PORT=8884
346+
if __name__ == "__main__":
347+
# Setup the server
348+
application.listen(PORT)
349+
tornado.ioloop.IOLoop.instance().start()
350+
351+
The `base.html` file can be used as base for all site pages which are for example implemented in the content block.
352+
353+
.. code-block:: html
354+
355+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
356+
<html lang="en">
357+
<html xmlns="http://www.w3.org/1999/xhtml">
358+
<head>
359+
<link rel="stylesheet" href="style.css" />
360+
<title>{{title}} - My Webpage</title>
361+
</head>
362+
<body>
363+
<div id="content">
364+
{# In the next line the content from the site.html template will be added #}
365+
{% block content %}{% endblock %}
366+
</div>
367+
<div id="footer">
368+
{% block footer %}
369+
&copy; Copyright 2013 by <a href="http://domain.invalid/">you</a>.
370+
{% endblock %}
371+
</div>
372+
</body>
373+
374+
375+
The next listing is our site page (`site.html`) loaded in the python app which extends `base.html`. The content block is
376+
automatically set into the corresponding block in the base.html page.
377+
378+
.. code-block:: html
379+
380+
<!{% extends "base.html" %}
381+
{% block content %}
382+
<p class="important">
383+
<div id="content">
384+
<h2>{{title}}</h2>
385+
<p>{{ list_title }}</p>
386+
<ul>
387+
{% for item in list %}
388+
<li>{{ item[0]}} : {{ item[1]}}</li>
389+
{% endfor %}
390+
</ul>
391+
</div>
392+
</p>
393+
{% endblock %}
394+
395+
272396
.. rubric:: References
273397

274398
.. [1] `The mod_python project is now officially dead <http://blog.dscpl.com.au/2010/06/modpython-project-is-now-officially.html>`_

0 commit comments

Comments
 (0)