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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Loops | 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/loops.txt">view source online</a></li>
</ul>
<h3>Table of Contents</h3>
<ul>
<li><a href="#for">For</a></li>
<li><a href="#range">Range</a></li>
<li><a href="#loop-variables">Loop Variables</a></li>
<li><a href="#cycling">Cycling</a></li>
<li><a href="#ifchanged">IfChanged</a></li>
<li><a href="#recursion">Recursion</a></li>
</ul>
</div>
<h1>Loops</h1>
<div id="page">
<p>Jinja provides two types of loops. The first one is the for loop:</p>
<div class="section">
<h2><a id="for" name="for">For</a></h2>
<p>Loop over each item in an array. For example, to display a list of users
given <tt class="docutils literal"><span class="pre">user_list</span></tt>:</p>
<pre class="literal-block">
<ul>
{% for user in user_list %}
<li><a href="/profile/{{ user.id }}/">{{ user.name }}</a></li>
{% endfor %}
</ul>
</pre>
<p>You can also loop over a list in reverse by using
<tt class="docutils literal"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">obj</span> <span class="pre">in</span> <span class="pre">list</span> <span class="pre">reversed</span> <span class="pre">%}</span></tt>.</p>
<p>You can also define a <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">elsefor</span> <span class="pre">%}</span></tt> statement, which gets rendered if
the for loop doesn't iterate:</p>
<pre class="literal-block">
<ul>
{% for user in userlist %}
<li>{{ user }}</li>
{% elsefor %}
<li><strong>no user found</strong></li>
{% endfor %}
</ul>
</pre>
</div>
<div class="section">
<h2><a id="range" name="range">Range</a></h2>
<p>range loops can be used to iterate through a list of numbers:</p>
<pre class="literal-block">
{% range number from 1 to 10 %}
{{ number }}
{% endrange %}
</pre>
<p>You can also step downwards:</p>
<pre class="literal-block">
{% range number from 10 downto 1 %}
{{ number }}
{% endrange %}
</pre>
<p>It is even possible to set the stepsize:</p>
<pre class="literal-block">
{% range number from 1 to 100 step 33 %}
{{ number }}
{% endrange %}
</pre>
</div>
<div class="section">
<h2><a id="loop-variables" name="loop-variables">Loop Variables</a></h2>
<p>These variables are available inside each <tt class="docutils literal"><span class="pre">for</span></tt> and <tt class="docutils literal"><span class="pre">range</span></tt>
loop:</p>
<table class="docutils">
<colgroup>
<col width="36%" />
<col width="64%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Variable</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">loop.counter</span></tt></td>
<td>The current iteration of the loop</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.counter0</span></tt></td>
<td>The current iteration of the loop,
starting counting by 0.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.revcounter</span></tt></td>
<td>The number of iterations from the end
of the loop.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.revcounter0</span></tt></td>
<td>The number of iterations from the end
of the loop, starting counting by 0.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.first</span></tt></td>
<td>True if first iteration.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.last</span></tt></td>
<td>True if last iteration.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.even</span></tt></td>
<td>True if current iteration is even.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.odd</span></tt></td>
<td>True if current iteration is odd.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">loop.parent</span></tt></td>
<td>The context of the parent loop.</td>
</tr>
</tbody>
</table>
</div>
<div class="section">
<h2><a id="cycling" name="cycling">Cycling</a></h2>
<p>Cycle among the given strings each time this tag is encountered.</p>
<p>Within a loop, cycles among the given strings each time through the loop:</p>
<pre class="literal-block">
{% for row in rows %}
<tr class="{% cycle 'row1', 'row2' inline %}">
...
</tr>
{% endfor %}
</pre>
<p>Or by cycling into a variable:</p>
<pre class="literal-block">
{% for row in rows %}
{% cycle rowclass through "row1", "row2" %}
<tr>
<td class="{{ rowclass }}">...</td>
<td class="{{ rowclass }}">...</td>
...
</tr>
{% endfor %}
</pre>
</div>
<div class="section">
<h2><a id="ifchanged" name="ifchanged">IfChanged</a></h2>
<p>Check if a value has changed from the last iteration of a loop.</p>
<p>The 'ifchanged' block tag is used within a loop. It checks its own rendered
contents against its previous state and only displays its content if the value
has changed:</p>
<pre class="literal-block">
<h1>Userlist</h1>
{% for user in users %}
{% ifchanged %}<h3>{{ user.type }}</h3>{% endifchanged %}
{{ user.name }}
{% endfor %}
</pre>
</div>
<div class="section">
<h2><a id="recursion" name="recursion">Recursion</a></h2>
<p>Often it's important to be able to use recursion. For example when you
want to build a sitemap or a tree.</p>
<p>In Jinja every <tt class="docutils literal"><span class="pre">for</span></tt> tag can be used to call it again with other
variables:</p>
<pre class="literal-block">
<ul class="sitemap">
{% for item in sitemap %}
<li><a href="{{ item.link }}">{{ item.title|e }}</a>{%
if item.items %}<ul>{% recurse item.items %}</ul></li>
{% endfor %}
</ul>
</pre>
<p>If you want to recurse in a higher loop you can use this syntax:</p>
<pre class="literal-block">
{% recurse item.items in loop.parent %}
</pre>
</div>
</div>
</body>
</html>
|