Skip to content

Commit 60cdeae

Browse files
author
tommy3001
committed
Tornado web server and Jinja2 template engine with example added
1 parent d084072 commit 60cdeae

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

docs/scenarios/web.rst

Lines changed: 130 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,127 @@ 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+
{# The next tag is a variable output: #}
295+
{{title}}
296+
{# Tag for a block, can be replaced through inheritance with other html code #}
297+
{% block head %}
298+
<h1>I'm the head!</h1>
299+
{% endblock %}
300+
{# Output of an array as an iteration #}
301+
{% for item in list %}
302+
<li>{{ item }}</li>
303+
{% endfor %}
304+
305+
306+
307+
The next listings is an example of a web site in combination with the tornado web server. Tornado is not very complicate
308+
to use.
309+
310+
.. code-block:: python
311+
312+
from jinja2 import Environment, FileSystemLoader
313+
TEMPLATE_FILE = "site.html"
314+
templateLoader = FileSystemLoader( searchpath="templates/" )
315+
templateEnv = Environment( loader=templateLoader )
316+
template = templateEnv.get_template(TEMPLATE_FILE)
317+
import tornado.ioloop
318+
import tornado.web
319+
#import jinja2
320+
from jinja2 import Environment, FileSystemLoader
321+
#load tamplate file templates/site.html
322+
TEMPLATE_FILE = "site.html"
323+
templateLoader = FileSystemLoader( searchpath="templates/" )
324+
templateEnv = Environment( loader=templateLoader )
325+
template = templateEnv.get_template(TEMPLATE_FILE)
326+
#import tornado
327+
import tornado.ioloop
328+
import tornado.web
329+
#list for famous movie rendering
330+
movie_list = [[1,"The Hitchhiker's Guide to the Galaxy"],[2,"Back to future"],[3,"Matrix"]]
331+
332+
# template.render() returns a string which contains the rendered html
333+
html_output = template.render(list=movie_list,
334+
title="Here is my favorite movie list")
335+
336+
#Handler for main page
337+
class MainHandler(tornado.web.RequestHandler):
338+
def get(self):
339+
#returns rendered template string to the browser request
340+
self.write(html_output)
341+
342+
# Assign handler to the server root (127.0.0.1:PORT/)
343+
application = tornado.web.Application([
344+
(r"/", MainHandler),
345+
])
346+
PORT=8884
347+
if __name__ == "__main__":
348+
# Setup the server
349+
application.listen(PORT)
350+
tornado.ioloop.IOLoop.instance().start()
351+
352+
The `base.html` file can be used as base for all site pages which are for example implemented in the content block.
353+
354+
.. code-block:: html
355+
356+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
357+
<html lang="en">
358+
<html xmlns="http://www.w3.org/1999/xhtml">
359+
<head>
360+
<link rel="stylesheet" href="style.css" />
361+
<title>{{title}} - My Webpage</title>
362+
</head>
363+
<body>
364+
<div id="content">
365+
{# In the next line the content from the site.html template will be added #}
366+
{% block content %}{% endblock %}
367+
</div>
368+
<div id="footer">
369+
{% block footer %}
370+
&copy; Copyright 2013 by <a href="http://domain.invalid/">you</a>.
371+
{% endblock %}
372+
</div>
373+
</body>
374+
375+
376+
The next listing is our site page (`site.html`) loaded in the python app which extends `base.html`. The content block is automatically
377+
set into the corresponding block in the base.html page.
378+
379+
.. code-block:: html
380+
381+
<!{% extends "base.html" %}
382+
{% block content %}
383+
<p class="important">
384+
<div id="content">
385+
<h2>{{title}}</h2>
386+
<p>{{ list_title }}</p>
387+
<ul>
388+
{% for item in list %}
389+
<li>{{ item[0]}} : {{ item[1]}}</li>
390+
{% endfor %}
391+
</ul>
392+
</div>
393+
</p>
394+
{% endblock %}
395+
396+
397+
398+
399+
400+
401+
272402
.. rubric:: References
273403

274404
.. [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)