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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Basic Usage | Jinja Documentation</title>
<style text="text/css">
body {
font-family: 'Arial', sans-serif;
margin: 1em;
padding: 0;
}
#navigation {
float: right;
margin: -1em -1em 1em 1em;
padding: 1em 2em 0 2em;
border: 1px solid #bbb;
border-width: 0 0 1px 1px;
background-color: #f8f8f8;
}
#page {
width: 45em;
}
a {
color: #d00;
}
a:hover {
color: #d40;
}
h1 {
font-size: 2em;
color: #d00;
margin: 0.5em 0 0.5em 0;
padding: 0;
}
h2 {
font-size: 1.7em;
color: #bd2121;
margin: 1em 0 0.5em 0;
}
h3 {
font-size: 1.3em;
color: #8a2424;
margin: 0.5em 0 0 0;
}
p {
margin: 0.5em 1em 0.5em 1em;
}
pre {
margin: 1em 0 1em 2em;
padding: 0.5em;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background-color: #f2f2f2;
overflow: auto;
}
li {
line-height: 1.4em;
}
hr {
margin: 1em;
padding: 0;
height: 1px!important;
background-color: #ccc;
border: none!important;
}
div.admonition {
margin: 1em 0 1em 1.5em;
padding: 0.5em 0.5em 0.5em 2em;
background-color: #f6e3e3;
border: 1px solid #d50000;
border-left: none;
border-right: none;
}
div.admonition p.admonition-title {
font-size: 1.1em;
color: #d40;
font-weight: bold;
margin: 0 0 0.5em -1em;
}
table {
border-collapse: collapse;
margin: 1em 2em 1em 1.5em;
}
table td, table th {
text-align: left;
border: 1px solid #eee;
padding: 0.3em;
}
table th {
background-color: #d00000;
color: white;
border: 1px solid #d00000;
border-bottom: 1px solid #eee;
}
</style>
</head>
<body>
<div id="navigation">
<h3>Documentation</h3>
<ul>
<li><a href="index.html">back to index</a></li>
<li><a href="http://wsgiarea.pocoo.org/repos/jinja/trunk/docs/source/basics.txt">view source online</a></li>
</ul>
<h3>Table of Contents</h3>
<ul>
<li><a href="#how-does-it-look-like">How does it look like?</a></li>
<li><a href="#context">Context</a></li>
<li><a href="#applying-filters">Applying Filters</a></li>
</ul>
</div>
<h1>Basic Usage</h1>
<div id="page">
<p>The Jinja template language is designed to strike a balance between content
and application logic.</p>
<div class="section">
<h2><a id="how-does-it-look-like" name="how-does-it-look-like">How does it look like?</a></h2>
<p>Here is a small example template:</p>
<pre class="literal-block">
<!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>
</pre>
</div>
<div class="section">
<h2><a id="context" name="context">Context</a></h2>
<p>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:</p>
<pre class="literal-block">
<pre>{% debug %}</pre>
</pre>
<p>You can print a context variable using <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">print</span> <span class="pre">var</span> <span class="pre">%}</span></tt> or the shortcut
form: <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">var</span> <span class="pre">}}</span></tt>.</p>
<p>A context isn't flat which means that each variable can has subvariables, as long
as it is representable as python data structure.</p>
<p>You can access attributes, dictionary values... using a dot ".":</p>
<pre class="literal-block">
{{ userlist.0.username }}
--> context['userlist'][0]['username']
{{ object.attribute }}
--> context['object']['attribute']
{{ item.caption }}
--> context['item']['caption']
</pre>
</div>
<div class="section">
<h2><a id="applying-filters" name="applying-filters">Applying Filters</a></h2>
<p>Jinja provides a simple but powerful filter system, which works like piping on
UNIX systems. Here a small example:</p>
<pre class="literal-block">
{{ variable|replace "search", "replace"|escapexml }}
</pre>
<p>You can put Whitespaces between filters if you like:</p>
<pre class="literal-block">
{{ variable | repalce "search", "replace" | escapexml }}
</pre>
<p>This will look for a variable <tt class="docutils literal"><span class="pre">variable</span></tt>, passes it to the filter <tt class="docutils literal"><span class="pre">replace</span></tt>
with the arguments "search" and "replace", and passes the result to the filter
<tt class="docutils literal"><span class="pre">escapexml</span></tt>.</p>
<p>For a list of available filters have a look at the <a class="reference" href="./filters.html">filter list</a>.</p>
</div>
</div>
</body>
</html>
|