@@ -286,17 +286,20 @@ Jinja2
286
286
language and thus can be used to generate any markup. It allows customization of filters, tags, tests and globals.
287
287
Unlike the template system implemented in the Django Framework it allows to call functions. The Code is staying under the BSD license.
288
288
289
- Here some important html tags in jinja2 :
289
+ Here some important html tags in Jinja2 :
290
290
291
291
.. code-block :: html
292
292
293
293
{# This is a comment #}
294
+
294
295
{# The next tag is a variable output: #}
295
296
{{title}}
297
+
296
298
{# Tag for a block, can be replaced through inheritance with other html code #}
297
299
{% block head %}
298
- <h1 >I'm the head!</h1 >
300
+ <h1 >This is the head!</h1 >
299
301
{% endblock %}
302
+
300
303
{# Output of an array as an iteration #}
301
304
{% for item in list %}
302
305
<li >{{ item }}</li >
@@ -309,34 +312,30 @@ to use.
309
312
310
313
.. code-block :: python
311
314
315
+ # import Jinja2
312
316
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
+
318
+ # import Tornado
317
319
import tornado.ioloop
318
320
import tornado.web
319
- # import jinja2
320
- from jinja2 import Environment, FileSystemLoader
321
- # load tamplate file templates/site.html
321
+
322
+ # Load tamplate file templates/site.html
322
323
TEMPLATE_FILE = " site.html"
323
324
templateLoader = FileSystemLoader( searchpath = " templates/" )
324
325
templateEnv = Environment( loader = templateLoader )
325
326
template = templateEnv.get_template(TEMPLATE_FILE )
326
- # import tornado
327
- import tornado.ioloop
328
- import tornado.web
329
- # list for famous movie rendering
327
+
328
+ # List for famous movie rendering
330
329
movie_list = [[1 ," The Hitchhiker's Guide to the Galaxy" ],[2 ," Back to future" ],[3 ," Matrix" ]]
331
330
332
331
# template.render() returns a string which contains the rendered html
333
332
html_output = template.render(list = movie_list,
334
333
title = " Here is my favorite movie list" )
335
334
336
- # Handler for main page
335
+ # Handler for main page
337
336
class MainHandler (tornado .web .RequestHandler ):
338
337
def get (self ):
339
- # returns rendered template string to the browser request
338
+ # Returns rendered template string to the browser request
340
339
self .write(html_output)
341
340
342
341
# Assign handler to the server root (127.0.0.1:PORT/)
@@ -373,8 +372,8 @@ The `base.html` file can be used as base for all site pages which are for exampl
373
372
</body >
374
373
375
374
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.
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.
378
377
379
378
.. code-block :: html
380
379
@@ -394,11 +393,6 @@ set into the corresponding block in the base.html page.
394
393
{% endblock %}
395
394
396
395
397
-
398
-
399
-
400
-
401
-
402
396
.. rubric :: References
403
397
404
398
.. [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