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.