File: basics.txt

package info (click to toggle)
jinja 0.9-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 412 kB
  • ctags: 485
  • sloc: python: 2,551; makefile: 40
file content (73 lines) | stat: -rw-r--r-- 2,094 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
===========
Basic Usage
===========

The Jinja template language is designed to strike a balance between content
and application logic.

How does it look like?
======================

Here is a small example template::

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title>My Webpage</title>
    </head
    <body>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption|escapexml }}</a></li>
        {% endfor %}
        </ul>

        <h1>My Webpage</h1>
        {{ variable }}
    </body>
    </html>

Context
=======

The application developer provides a list of variables called context. Normally
there should be a documentation but if you want to know the content of the current
context you can add this to your template::

    <pre>{% debug %}</pre>

You can print a context variable using ``{% print var %}`` or the shortcut
form: ``{{ var }}``.

A context isn't flat which means that each variable can has subvariables, as long
as it is representable as python data structure.

You can access attributes, dictionary values... using a dot "."::

    {{ userlist.0.username }}
        --> context['userlist'][0]['username']
    {{ object.attribute }}
        --> context['object']['attribute']
    {{ item.caption }}
        --> context['item']['caption']

Applying Filters
================

Jinja provides a simple but powerful filter system, which works like piping on
UNIX systems. Here a small example::

    {{ variable|replace "search", "replace"|escapexml }}

You can put Whitespaces between filters if you like::

    {{ variable | repalce "search", "replace" | escapexml }}

This will look for a variable ``variable``, passes it to the filter ``replace``
with the arguments "search" and "replace", and passes the result to the filter
``escapexml``.

For a list of available filters have a look at the `filter list`_.

.. _filter list: filters.txt