| 1 |
================================================== |
|---|
| 2 |
The Django template language: For template authors |
|---|
| 3 |
================================================== |
|---|
| 4 |
|
|---|
| 5 |
Django's template language is designed to strike a balance between power and |
|---|
| 6 |
ease. It's designed to feel comfortable to those used to working with HTML. If |
|---|
| 7 |
you have any exposure to other text-based template languages, such as Smarty_ |
|---|
| 8 |
or CheetahTemplate_, you should feel right at home with Django's templates. |
|---|
| 9 |
|
|---|
| 10 |
.. _Smarty: http://smarty.php.net/ |
|---|
| 11 |
.. _CheetahTemplate: http://www.cheetahtemplate.org/ |
|---|
| 12 |
|
|---|
| 13 |
Templates |
|---|
| 14 |
========= |
|---|
| 15 |
|
|---|
| 16 |
A template is simply a text file. All Django templates, by convention, have |
|---|
| 17 |
".html" extensions, but they can generate any text-based format (HTML, XML, |
|---|
| 18 |
CSV, etc.). |
|---|
| 19 |
|
|---|
| 20 |
A template contains **variables**, which get replaced with values when the |
|---|
| 21 |
template is evaluated, and **tags**, which control the logic of the template. |
|---|
| 22 |
|
|---|
| 23 |
Below is a minimal template that illustrates a few basics. Each element will be |
|---|
| 24 |
explained later in this document.:: |
|---|
| 25 |
|
|---|
| 26 |
{% extends "base_generic" %} |
|---|
| 27 |
|
|---|
| 28 |
{% block title %}{{ section.title }}{% endblock %} |
|---|
| 29 |
|
|---|
| 30 |
{% block content %} |
|---|
| 31 |
<h1>{{ section.title }}</h1> |
|---|
| 32 |
|
|---|
| 33 |
{% for story in story_list %} |
|---|
| 34 |
<h2> |
|---|
| 35 |
<a href="{{ story.get_absolute_url }}"> |
|---|
| 36 |
{{ story.headline|upper }} |
|---|
| 37 |
</a> |
|---|
| 38 |
</h2> |
|---|
| 39 |
<p>{{ story.tease|truncatewords:"100" }}</p> |
|---|
| 40 |
{% endfor %} |
|---|
| 41 |
{% endblock %} |
|---|
| 42 |
|
|---|
| 43 |
.. admonition:: Philosophy |
|---|
| 44 |
|
|---|
| 45 |
Why use a text-based template instead of an XML-based one (like Zope's |
|---|
| 46 |
TAL)? We wanted Django's template language to be usable for more than |
|---|
| 47 |
just XML/HTML templates. At World Online, we use it for e-mails, |
|---|
| 48 |
JavaScript and CSV. You can use the template language for any text-based |
|---|
| 49 |
format. |
|---|
| 50 |
|
|---|
| 51 |
Variables |
|---|
| 52 |
========= |
|---|
| 53 |
|
|---|
| 54 |
Variables look like this: ``{{ variable }}``. When the template engine |
|---|
| 55 |
encounters a variable, it evaluates that variable and replaces it with the |
|---|
| 56 |
result. |
|---|
| 57 |
|
|---|
| 58 |
Use a dot (``.``) to access attributes of a variable. |
|---|
| 59 |
|
|---|
| 60 |
.. admonition:: Behind the scenes |
|---|
| 61 |
|
|---|
| 62 |
Technically, when the template system encounters a dot, it tries the |
|---|
| 63 |
following lookups, in this order: |
|---|
| 64 |
|
|---|
| 65 |
* Dictionary lookup |
|---|
| 66 |
* Attribute lookup |
|---|
| 67 |
* Method call |
|---|
| 68 |
* List-index lookup |
|---|
| 69 |
|
|---|
| 70 |
In the above example, ``{{ section.title }}`` will be replaced with the |
|---|
| 71 |
``title`` attribute of the ``section`` object. |
|---|
| 72 |
|
|---|
| 73 |
If you use a variable that doesn't exist, it will be silently ignored. The |
|---|
| 74 |
variable will be replaced by nothingness. |
|---|
| 75 |
|
|---|
| 76 |
See `Using the built-in reference`_, below, for help on finding what variables |
|---|
| 77 |
are available in a given template. |
|---|
| 78 |
|
|---|
| 79 |
You can modify variables for display by using **filters**. |
|---|
| 80 |
|
|---|
| 81 |
Filters |
|---|
| 82 |
======= |
|---|
| 83 |
|
|---|
| 84 |
Filters look like this: ``{{ name|lower }}``. This displays the value of the |
|---|
| 85 |
``{{ name }}`` variable after being filtered through the ``lower`` filter, |
|---|
| 86 |
which converts text to lowercase. Use a pipe (``|``) to apply a filter. |
|---|
| 87 |
|
|---|
| 88 |
Filters can be "chained." The output of one filter applied to the next: |
|---|
| 89 |
``{{ text|escape|linebreaks }}`` is a common idiom for escaping text contents |
|---|
| 90 |
and then converting line breaks to ``<p>`` tags. |
|---|
| 91 |
|
|---|
| 92 |
Certain filters take arguments. A filter argument looks like this: |
|---|
| 93 |
``{{ bio|truncatewords:"30" }}``. This will display the first 30 words of the |
|---|
| 94 |
``bio`` variable. Filter arguments always are in double quotes. |
|---|
| 95 |
|
|---|
| 96 |
The `Built-in filter reference`_ below describes all the built-in filters. |
|---|
| 97 |
|
|---|
| 98 |
Tags |
|---|
| 99 |
==== |
|---|
| 100 |
|
|---|
| 101 |
Tags look like this: ``{% tag %}``. Tags are more complex than variables: Some |
|---|
| 102 |
create text in the output, some control flow by performing loops or logic, and |
|---|
| 103 |
some load external information into the template to be used by later variables. |
|---|
| 104 |
|
|---|
| 105 |
Some tags require beginning and ending tags (i.e. |
|---|
| 106 |
``{% tag %} ... tag contents ... {% endtag %}``). The `Built-in tag reference`_ |
|---|
| 107 |
below describes all the built-in tags. You can create your own tags, if you |
|---|
| 108 |
know how to write Python code. |
|---|
| 109 |
|
|---|
| 110 |
Template inheritance |
|---|
| 111 |
==================== |
|---|
| 112 |
|
|---|
| 113 |
The most powerful -- and thus the most complex -- part of Django's template |
|---|
| 114 |
engine is template inheritance. Template inheritance allows you to build a base |
|---|
| 115 |
"skeleton" template that contains all the common elements of your site and |
|---|
| 116 |
defines **blocks** that child templates can override. |
|---|
| 117 |
|
|---|
| 118 |
It's easiest to understand template inheritance by starting with an example:: |
|---|
| 119 |
|
|---|
| 120 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|---|
| 121 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 122 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
|---|
| 123 |
<head> |
|---|
| 124 |
<link rel="stylesheet" href="style.css" /> |
|---|
| 125 |
<title>{% block title %}My amazing site{% endblock %}</title> |
|---|
| 126 |
</head> |
|---|
| 127 |
|
|---|
| 128 |
<body> |
|---|
| 129 |
<div id="sidebar"> |
|---|
| 130 |
{% block sidebar %} |
|---|
| 131 |
<ul> |
|---|
| 132 |
<li><a href="/">Home</a></li> |
|---|
| 133 |
<li><a href="/blog/">Blog</a></li> |
|---|
| 134 |
</ul> |
|---|
| 135 |
{% endblock %} |
|---|
| 136 |
</div> |
|---|
| 137 |
|
|---|
| 138 |
<div id="content"> |
|---|
| 139 |
{% block content %}{% endblock %} |
|---|
| 140 |
</div> |
|---|
| 141 |
</body> |
|---|
| 142 |
|
|---|
| 143 |
This template, which we'll call ``base.html``, defines a simple HTML skeleton |
|---|
| 144 |
document that you might use for a simple two-column page. It's the job of |
|---|
| 145 |
"child" templates to fill the empty blocks with content. |
|---|
| 146 |
|
|---|
| 147 |
In this example, the ``{% block %}`` tag defines three blocks that child |
|---|
| 148 |
templates can fill in. All the ``block`` tag does is to tell the template |
|---|
| 149 |
engine that a child template may override those portions of the template. |
|---|
| 150 |
|
|---|
| 151 |
A child template might look like this:: |
|---|
| 152 |
|
|---|
| 153 |
{% extends "base" %} |
|---|
| 154 |
|
|---|
| 155 |
{% block title %}My amazing blog{% endblock %} |
|---|
| 156 |
|
|---|
| 157 |
{% block content %} |
|---|
| 158 |
{% for entry in blog_entries %} |
|---|
| 159 |
<h2>{{ entry.title }}</h2> |
|---|
| 160 |
<p>{{ entry.body }}</p> |
|---|
| 161 |
{% endfor %} |
|---|
| 162 |
{% endblock %} |
|---|
| 163 |
|
|---|
| 164 |
The ``{% extends %}`` tag is the key here. It tells the template engine that |
|---|
| 165 |
this template "extends" another template. When the template system evaluates |
|---|
| 166 |
this template, first it locates the parent -- in this case, "base" (note the |
|---|
| 167 |
lack of an ".html" extension in the ``{% extends %}`` tag). |
|---|
| 168 |
|
|---|
| 169 |
At that point, the template engine will notice the three blocks in |
|---|
| 170 |
``base.html`` and replace those blocks with the contents of the child template. |
|---|
| 171 |
Depending on the value of ``blog_entries``, the output might look like:: |
|---|
| 172 |
|
|---|
| 173 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|---|
| 174 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 175 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
|---|
| 176 |
<head> |
|---|
| 177 |
<link rel="stylesheet" href="style.css" /> |
|---|
| 178 |
<title>My amazing blog</title> |
|---|
| 179 |
</head> |
|---|
| 180 |
|
|---|
| 181 |
<body> |
|---|
| 182 |
<div id="sidebar"> |
|---|
| 183 |
<ul> |
|---|
| 184 |
<li><a href="/">Home</a></li> |
|---|
| 185 |
<li><a href="/blog/">Blog</a></li> |
|---|
| 186 |
</ul> |
|---|
| 187 |
</div> |
|---|
| 188 |
|
|---|
| 189 |
<div id="content"> |
|---|
| 190 |
<h2>Entry one</h2> |
|---|
| 191 |
<p>This is my first entry.</p> |
|---|
| 192 |
|
|---|
| 193 |
<h2>Entry two</h2> |
|---|
| 194 |
<p>This is my second entry.</p> |
|---|
| 195 |
</div> |
|---|
| 196 |
</body> |
|---|
| 197 |
|
|---|
| 198 |
Note that since the child template didn't define the ``sidebar`` block, the |
|---|
| 199 |
value from the parent template is used instead. Content within a ``{% block %}`` |
|---|
| 200 |
tag in a parent template is always used as a fallback. |
|---|
| 201 |
|
|---|
| 202 |
Template inheritance isn't limited to a single level. Multi-level inheritance |
|---|
| 203 |
is possible and, indeed, quite useful. |
|---|
| 204 |
|
|---|
| 205 |
Here are some tips for working with inheritance: |
|---|
| 206 |
|
|---|
| 207 |
* More ``{% block %}`` tags in your base templates are better. Remember, |
|---|
| 208 |
child templates don't have to define all parent blocks, so you can fill |
|---|
| 209 |
in reasonable defaults in a number of blocks, then only define the ones |
|---|
| 210 |
you need later. |
|---|
| 211 |
|
|---|
| 212 |
* If you find yourself duplicating content in a number of templates, it |
|---|
| 213 |
probably means you should move that content to a ``{% block %}`` in a |
|---|
| 214 |
parent template. |
|---|
| 215 |
|
|---|
| 216 |
* The recommended template layout is to use three levels: a single base |
|---|
| 217 |
template for the entire site, a set of mid-level templates for each |
|---|
| 218 |
section of the site, and then the individual templates for each view. |
|---|
| 219 |
This maximizes code reuse and makes it easier to add items to shared |
|---|
| 220 |
content areas (such as section-wide navigation). |
|---|
| 221 |
|
|---|
| 222 |
* If you need to get the content of the block from the parent template, |
|---|
| 223 |
the ``{{ block.super }}`` variable will do the trick. This is useful |
|---|
| 224 |
if you want to add to the contents of a parent block instead of |
|---|
| 225 |
completely overriding it. |
|---|
| 226 |
|
|---|
| 227 |
Finally, note that you can't define multiple ``{% block %}`` tags with the same |
|---|
| 228 |
name in the same template. This limitation exists because a block tag works in |
|---|
| 229 |
"both" directions. That is, a block tag doesn't just provide a hole to fill -- |
|---|
| 230 |
it also defines the content that fills the hole in the *parent*. If there were |
|---|
| 231 |
two similarly-named ``{% block %}`` tags in a template, that template's parent |
|---|
| 232 |
wouldn't know which one of the blocks' content to use. |
|---|
| 233 |
|
|---|
| 234 |
Using the built-in reference |
|---|
| 235 |
============================ |
|---|
| 236 |
|
|---|
| 237 |
Because Django can be used to develop any sort of site, the tags, filters and |
|---|
| 238 |
variables available are different depending on the application. To make it |
|---|
| 239 |
easy to figure out what's available in a given site, the admin interface has a |
|---|
| 240 |
complete reference of all the template goodies available to that site. |
|---|
| 241 |
|
|---|
| 242 |
The reference is integrated into the administration interface for your site(s) |
|---|
| 243 |
and is divided into 4 sections: tags, filters, models, and views. |
|---|
| 244 |
|
|---|
| 245 |
The **tags** and **filters** sections describe all the built-in tags (in fact, |
|---|
| 246 |
the tag and filter references below come directly from those pages) as well as |
|---|
| 247 |
any custom tag or filter libraries available. |
|---|
| 248 |
|
|---|
| 249 |
The **views** page is the most valuable. Each URL in your site has a separate |
|---|
| 250 |
entry here, and clicking on a URL will show you: |
|---|
| 251 |
|
|---|
| 252 |
* The name of the view function that generates that view. |
|---|
| 253 |
* A short description of what the view does. |
|---|
| 254 |
* The **context**, or a list of variables available in the view. |
|---|
| 255 |
* The name of the template or templates that are used for that view. |
|---|
| 256 |
|
|---|
| 257 |
Each view documentation page also has a bookmarklet that you can use to jump |
|---|
| 258 |
from any page to the documentation page for that view. |
|---|
| 259 |
|
|---|
| 260 |
Because Django generally revolves around database objects, the **models** |
|---|
| 261 |
section of the documentation page describes each type of object in the system |
|---|
| 262 |
along with all the fields available on that object. |
|---|
| 263 |
|
|---|
| 264 |
Taken together, the documentation pages should tell you every tag, filter, |
|---|
| 265 |
variable and object available to you in a given template. |
|---|
| 266 |
|
|---|
| 267 |
Custom tag and filter libraries |
|---|
| 268 |
=============================== |
|---|
| 269 |
|
|---|
| 270 |
Certain applications provide custom tag and filter libraries. To access them in |
|---|
| 271 |
a template, use the ``{% load %}`` tag:: |
|---|
| 272 |
|
|---|
| 273 |
{% load comments %} |
|---|
| 274 |
|
|---|
| 275 |
{% comment_form for blogs.entries entry.id with is_public yes %} |
|---|
| 276 |
|
|---|
| 277 |
In the above, the ``load`` tag loads the ``comments`` tag library, which then |
|---|
| 278 |
makes the ``comment_form`` tag available for use. Consult the documentation |
|---|
| 279 |
area in your admin to find the list of custom libraries in your installation. |
|---|
| 280 |
|
|---|
| 281 |
Built-in tag and filter reference |
|---|
| 282 |
================================= |
|---|
| 283 |
|
|---|
| 284 |
For those without an admin site available, reference for the stock tags and |
|---|
| 285 |
filters follows. Because Django is highly customizable, the reference in your |
|---|
| 286 |
admin should be considered the final word on what tags and filters are |
|---|
| 287 |
available, and what they do. |
|---|
| 288 |
|
|---|
| 289 |
Built-in tag reference |
|---|
| 290 |
---------------------- |
|---|
| 291 |
|
|---|
| 292 |
block |
|---|
| 293 |
~~~~~ |
|---|
| 294 |
|
|---|
| 295 |
Define a block that can be overridden by child templates. See |
|---|
| 296 |
`Template inheritance`_ for more information. |
|---|
| 297 |
|
|---|
| 298 |
comment |
|---|
| 299 |
~~~~~~~ |
|---|
| 300 |
|
|---|
| 301 |
Ignore everything between ``{% comment %}`` and ``{% endcomment %}`` |
|---|
| 302 |
|
|---|
| 303 |
cycle |
|---|
| 304 |
~~~~~ |
|---|
| 305 |
|
|---|
| 306 |
Cycle among the given strings each time this tag is encountered. |
|---|
| 307 |
|
|---|
| 308 |
Within a loop, cycles among the given strings each time through the loop:: |
|---|
| 309 |
|
|---|
| 310 |
{% for o in some_list %} |
|---|
| 311 |
<tr class="{% cycle row1,row2 %}"> |
|---|
| 312 |
... |
|---|
| 313 |
</tr> |
|---|
| 314 |
{% endfor %} |
|---|
| 315 |
|
|---|
| 316 |
Outside of a loop, give the values a unique name the first time you call it, |
|---|
| 317 |
then use that name each successive time through:: |
|---|
| 318 |
|
|---|
| 319 |
<tr class="{% cycle row1,row2,row3 as rowcolors %}">...</tr> |
|---|
| 320 |
<tr class="{% cycle rowcolors %}">...</tr> |
|---|
| 321 |
<tr class="{% cycle rowcolors %}">...</tr> |
|---|
| 322 |
|
|---|
| 323 |
You can use any number of values, separated by commas. Make sure not to put |
|---|
| 324 |
spaces between the values -- only commas. |
|---|
| 325 |
|
|---|
| 326 |
debug |
|---|
| 327 |
~~~~~ |
|---|
| 328 |
|
|---|
| 329 |
Output a whole load of debugging information, including the current context and |
|---|
| 330 |
imported modules. |
|---|
| 331 |
|
|---|
| 332 |
extends |
|---|
| 333 |
~~~~~~~ |
|---|
| 334 |
|
|---|
| 335 |
Signal that this template extends a parent template. |
|---|
| 336 |
|
|---|
| 337 |
This tag may be used in two ways: ``{% extends "base" %}`` (with quotes) uses |
|---|
| 338 |
the literal value "base" as the name of the parent template to extend, or ``{% |
|---|
| 339 |
extends variable %}`` uses the value of ``variable`` as the name of the parent |
|---|
| 340 |
template to extend. |
|---|
| 341 |
|
|---|
| 342 |
See `Template inheritance`_ for more information. |
|---|
| 343 |
|
|---|
| 344 |
filter |
|---|
| 345 |
~~~~~~ |
|---|
| 346 |
|
|---|
| 347 |
Filter the contents of the variable through variable filters. |
|---|
| 348 |
|
|---|
| 349 |
Filters can also be piped through each other, and they can have arguments -- |
|---|
| 350 |
just like in variable syntax. |
|---|
| 351 |
|
|---|
| 352 |
Sample usage:: |
|---|
| 353 |
|
|---|
| 354 |
{% filter escape|lower %} |
|---|
| 355 |
This text will be HTML-escaped, and will appear in all lowercase. |
|---|
| 356 |
{% endfilter %} |
|---|
| 357 |
|
|---|
| 358 |
firstof |
|---|
| 359 |
~~~~~~~ |
|---|
| 360 |
|
|---|
| 361 |
Outputs the first variable passed that is not False. Outputs nothing if all the |
|---|
| 362 |
passed variables are False. |
|---|
| 363 |
|
|---|
| 364 |
Sample usage:: |
|---|
| 365 |
|
|---|
| 366 |
{% firstof var1 var2 var3 %} |
|---|
| 367 |
|
|---|
| 368 |
This is equivalent to:: |
|---|
| 369 |
|
|---|
| 370 |
{% if var1 %} |
|---|
| 371 |
{{ var1 }} |
|---|
| 372 |
{% else %}{% if var2 %} |
|---|
| 373 |
{{ var2 }} |
|---|
| 374 |
{% else %}{% if var3 %} |
|---|
| 375 |
{{ var3 }} |
|---|
| 376 |
{% endif %}{% endif %}{% endif %} |
|---|
| 377 |
|
|---|
| 378 |
for |
|---|
| 379 |
~~~ |
|---|
| 380 |
|
|---|
| 381 |
Loop over each item in an array. For example, to display a list of athletes |
|---|
| 382 |
given ``athlete_list``:: |
|---|
| 383 |
|
|---|
| 384 |
<ul> |
|---|
| 385 |
{% for athlete in athlete_list %} |
|---|
| 386 |
<li>{{ athlete.name }}</li> |
|---|
| 387 |
{% endfor %} |
|---|
| 388 |
</ul> |
|---|
| 389 |
|
|---|
| 390 |
You can also loop over a list in reverse by using ``{% for obj in list reversed %}``. |
|---|
| 391 |
|
|---|
| 392 |
The for loop sets a number of variables available within the loop: |
|---|
| 393 |
|
|---|
| 394 |
========================== ================================================ |
|---|
| 395 |
Variable Description |
|---|
| 396 |
========================== ================================================ |
|---|
| 397 |
``forloop.counter`` The current iteration of the loop (1-indexed) |
|---|
| 398 |
``forloop.counter0`` The current iteration of the loop (0-indexed) |
|---|
| 399 |
``forloop.revcounter`` The number of iterations from the end of the |
|---|
| 400 |
loop (1-indexed) |
|---|
| 401 |
``forloop.revcounter0`` The number of iterations from the end of the |
|---|
| 402 |
loop (0-indexed) |
|---|
| 403 |
``forloop.first`` True if this is the first time through the loop |
|---|
| 404 |
``forloop.last`` True if this is the last time through the loop |
|---|
| 405 |
``forloop.parentloop`` For nested loops, this is the loop "above" the |
|---|
| 406 |
current one |
|---|
| 407 |
========================== ================================================ |
|---|
| 408 |
|
|---|
| 409 |
if |
|---|
| 410 |
~~ |
|---|
| 411 |
|
|---|
| 412 |
The ``{% if %}`` tag evaluates a variable, and if that variable is "true" (i.e. |
|---|
| 413 |
exists, is not empty, and is not a false boolean value) the contents of the |
|---|
| 414 |
block are output:: |
|---|
| 415 |
|
|---|
| 416 |
{% if athlete_list %} |
|---|
| 417 |
Number of athletes: {{ athlete_list|length }} |
|---|
| 418 |
{% else %} |
|---|
| 419 |
No athletes. |
|---|
| 420 |
{% endif %} |
|---|
| 421 |
|
|---|
| 422 |
In the above, if ``athlete_list`` is not empty, the number of athletes will be |
|---|
| 423 |
displayed by the ``{{ athlete_list|length }}`` variable. |
|---|
| 424 |
|
|---|
| 425 |
As you can see, the ``if`` tag can take an option ``{% else %}`` clause that |
|---|
| 426 |
will be displayed if the test fails. |
|---|
| 427 |
|
|---|
| 428 |
``if`` tags may use ``or`` or ``not`` to test a number of variables or to negate |
|---|
| 429 |
a given variable:: |
|---|
| 430 |
|
|---|
| 431 |
{% if not athlete_list %} |
|---|
| 432 |
There are no athletes. |
|---|
| 433 |
{% endif %} |
|---|
| 434 |
|
|---|
| 435 |
{% if athlete_list or coach_list %} |
|---|
| 436 |
There are some athletes or some coaches. |
|---|
| 437 |
{% endif %} |
|---|
| 438 |
|
|---|
| 439 |
{% if not athlete_list or coach_list %} |
|---|
| 440 |
There are no athletes or there are some coaches (OK, so |
|---|
| 441 |
writing English translations of boolean logic sounds |
|---|
| 442 |
stupid; it's not my fault). |
|---|
| 443 |
{% endif %} |
|---|
| 444 |
|
|---|
| 445 |
For simplicity, ``if`` tags do not allow ``and`` clauses; use nested ``if`` |
|---|
| 446 |
tags instead:: |
|---|
| 447 |
|
|---|
| 448 |
{% if athlete_list %} |
|---|
| 449 |
{% if coach_list %} |
|---|
| 450 |
Number of athletes: {{ athlete_list|length }}. |
|---|
| 451 |
Number of coaches: {{ coach_list|length }}. |
|---|
| 452 |
{% endif %} |
|---|
| 453 |
{% endif %} |
|---|
| 454 |
|
|---|
| 455 |
ifchanged |
|---|
| 456 |
~~~~~~~~~ |
|---|
| 457 |
|
|---|
| 458 |
Check if a value has changed from the last iteration of a loop. |
|---|
| 459 |
|
|---|
| 460 |
The 'ifchanged' block tag is used within a loop. It checks its own rendered |
|---|
| 461 |
contents against its previous state and only displays its content if the value |
|---|
| 462 |
has changed:: |
|---|
| 463 |
|
|---|
| 464 |
<h1>Archive for {{ year }}</h1> |
|---|
| 465 |
|
|---|
| 466 |
{% for day in days %} |
|---|
| 467 |
{% ifchanged %}<h3>{{ day|date:"F" }}</h3>{% endifchanged %} |
|---|
| 468 |
<a href="{{ day|date:"M/d"|lower }}/">{{ day|date:"j" }}</a> |
|---|
| 469 |
{% endfor %} |
|---|
| 470 |
|
|---|
| 471 |
ifequal |
|---|
| 472 |
~~~~~~~ |
|---|
| 473 |
|
|---|
| 474 |
Output the contents of the block if the two arguments equal each other. |
|---|
| 475 |
|
|---|
| 476 |
Example:: |
|---|
| 477 |
|
|---|
| 478 |
{% ifequal user.id comment.user_id %} |
|---|
| 479 |
... |
|---|
| 480 |
{% endifequal %} |
|---|
| 481 |
|
|---|
| 482 |
As in the ``{% if %}`` tag, an ``{% else %}`` clause is optional. |
|---|
| 483 |
|
|---|
| 484 |
The arguments can be hard-coded strings, so the following is valid:: |
|---|
| 485 |
|
|---|
| 486 |
{% ifequal user.username "adrian" %} |
|---|
| 487 |
... |
|---|
| 488 |
{% endifequal %} |
|---|
| 489 |
|
|---|
| 490 |
ifnotequal |
|---|
| 491 |
~~~~~~~~~~ |
|---|
| 492 |
|
|---|
| 493 |
Just like ``ifequal``, except it tests that the two arguments are not equal. |
|---|
| 494 |
|
|---|
| 495 |
load |
|---|
| 496 |
~~~~ |
|---|
| 497 |
|
|---|
| 498 |
Load a custom template tag set. |
|---|
| 499 |
|
|---|
| 500 |
See `Custom tag and filter libraries`_ for more information. |
|---|
| 501 |
|
|---|
| 502 |
now |
|---|
| 503 |
~~~ |
|---|
| 504 |
|
|---|
| 505 |
Display the date, formatted according to the given string. |
|---|
| 506 |
|
|---|
| 507 |
Uses the same format as PHP's ``date()`` function (http://php.net/date) |
|---|
| 508 |
with some custom extensions. |
|---|
| 509 |
|
|---|
| 510 |
Available format strings: |
|---|
| 511 |
|
|---|
| 512 |
================ ======================================== ===================== |
|---|
| 513 |
Format character Description Example output |
|---|
| 514 |
================ ======================================== ===================== |
|---|
| 515 |
a ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'`` |
|---|
| 516 |
this is slightly different than PHP's |
|---|
| 517 |
output, because this includes periods |
|---|
| 518 |
to match Associated Press style.) |
|---|
| 519 |
A ``'AM'`` or ``'PM'``. ``'AM'`` |
|---|
| 520 |
B Not implemented. |
|---|
| 521 |
d Day of the month, 2 digits with ``'01'`` to ``'31'`` |
|---|
| 522 |
leading zeros. |
|---|
| 523 |
D Day of the week, textual, 3 letters. ``'Fri'`` |
|---|
| 524 |
f Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'`` |
|---|
| 525 |
with minutes left off if they're zero. |
|---|
| 526 |
Proprietary extension. |
|---|
| 527 |
F Month, textual, long. ``'January'`` |
|---|
| 528 |
g Hour, 12-hour format without leading ``'1'`` to ``'12'`` |
|---|
| 529 |
zeros. |
|---|
| 530 |
G Hour, 24-hour format without leading ``'0'`` to ``'23'`` |
|---|
| 531 |
zeros. |
|---|
| 532 |
h Hour, 12-hour format. ``'01'`` to ``'12'`` |
|---|
| 533 |
H Hour, 24-hour format. ``'00'`` to ``'23'`` |
|---|
| 534 |
i Minutes. ``'00'`` to ``'59'`` |
|---|
| 535 |
I Not implemented. |
|---|
| 536 |
j Day of the month without leading ``'1'`` to ``'31'`` |
|---|
| 537 |
zeros. |
|---|
| 538 |
l Day of the week, textual, long. ``'Friday'`` |
|---|
| 539 |
L Boolean for whether it's a leap year. ``True`` or ``False`` |
|---|
| 540 |
m Month, 2 digits with leading zeros. ``'01'`` to ``'12'`` |
|---|
| 541 |
M Month, textual, 3 letters. ``'Jan'`` |
|---|
| 542 |
n Month without leading zeros. ``'1'`` to ``'12'`` |
|---|
| 543 |
N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'`` |
|---|
| 544 |
style. Proprietary extension. |
|---|
| 545 |
O Difference to Greenwich time in hours. ``'+0200'`` |
|---|
| 546 |
P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'`` |
|---|
| 547 |
'a.m.'/'p.m.', with minutes left off |
|---|
| 548 |
if they're zero and the special-case |
|---|
| 549 |
strings 'midnight' and 'noon' if |
|---|
| 550 |
appropriate. Proprietary extension. |
|---|
| 551 |
r RFC 822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'`` |
|---|
| 552 |
s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'`` |
|---|
| 553 |
S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'`` |
|---|
| 554 |
month, 2 characters. |
|---|
| 555 |
t Number of days in the given month. ``28`` to ``31`` |
|---|
| 556 |
T Time zone of this machine. ``'EST'``, ``'MDT'`` |
|---|
| 557 |
U Not implemented. |
|---|
| 558 |
w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday) |
|---|
| 559 |
leading zeros. |
|---|
| 560 |
W ISO-8601 week number of year, with ``1``, ``23`` |
|---|
| 561 |
weeks starting on Monday. |
|---|
| 562 |
y Year, 2 digits. ``'99'`` |
|---|
| 563 |
Y Year, 4 digits. ``'1999'`` |
|---|
| 564 |
z Day of the year. ``0`` to ``365`` |
|---|
| 565 |
Z Time zone offset in seconds. The ``-43200`` to ``43200`` |
|---|
| 566 |
offset for timezones west of UTC is |
|---|
| 567 |
always negative, and for those east of |
|---|
| 568 |
UTC is always positive. |
|---|
| 569 |
================ ======================================== ===================== |
|---|
| 570 |
|
|---|
| 571 |
Example:: |
|---|
| 572 |
|
|---|
| 573 |
It is {% now "jS F Y H:i" %} |
|---|
| 574 |
|
|---|
| 575 |
Note that you can backslash-escape a format string if you want to use the |
|---|
| 576 |
"raw" value. In this example, "f" is backslash-escaped, because otherwise |
|---|
| 577 |
"f" is a format string that displays the time. The "o" doesn't need to be |
|---|
| 578 |
escaped, because it's not a format character.:: |
|---|
| 579 |
|
|---|
| 580 |
It is the {% "jS o\f F" %} |
|---|
| 581 |
|
|---|
| 582 |
(Displays "It is the 4th of September" %} |
|---|
| 583 |
|
|---|
| 584 |
regroup |
|---|
| 585 |
~~~~~~~ |
|---|
| 586 |
|
|---|
| 587 |
Regroup a list of alike objects by a common attribute. |
|---|
| 588 |
|
|---|
| 589 |
This complex tag is best illustrated by use of an example: say that ``people`` |
|---|
| 590 |
is a list of ``Person`` objects that have ``first_name``, ``last_name``, and |
|---|
| 591 |
``gender`` attributes, and you'd like to display a list that looks like: |
|---|
| 592 |
|
|---|
| 593 |
* Male: |
|---|
| 594 |
* George Bush |
|---|
| 595 |
* Bill Clinton |
|---|
| 596 |
* Female: |
|---|
| 597 |
* Margaret Thatcher |
|---|
| 598 |
* Condoleezza Rice |
|---|
| 599 |
* Unknown: |
|---|
| 600 |
* Pat Smith |
|---|
| 601 |
|
|---|
| 602 |
The following snippet of template code would accomplish this dubious task:: |
|---|
| 603 |
|
|---|
| 604 |
{% regroup people by gender as grouped %} |
|---|
| 605 |
<ul> |
|---|
| 606 |
{% for group in grouped %} |
|---|
| 607 |
<li>{{ group.grouper }} |
|---|
| 608 |
<ul> |
|---|
| 609 |
{% for item in group.list %} |
|---|
| 610 |
<li>{{ item }}</li> |
|---|
| 611 |
{% endfor %} |
|---|
| 612 |
</ul> |
|---|
| 613 |
{% endfor %} |
|---|
| 614 |
</ul> |
|---|
| 615 |
|
|---|
| 616 |
As you can see, ``{% regroup %}`` populates a variable with a list of objects |
|---|
| 617 |
with ``grouper`` and ``list`` attributes. ``grouper`` contains the item that |
|---|
| 618 |
was grouped by; ``list`` contains the list of objects that share that |
|---|
| 619 |
``grouper``. In this case, ``grouper`` would be ``Male``, ``Female`` and |
|---|
| 620 |
``Unknown``, and ``list`` is the list of people with those genders. |
|---|
| 621 |
|
|---|
| 622 |
Note that ``{% regroup %}`` does not work when the list to be grouped is not |
|---|
| 623 |
sorted by the key you are grouping by! This means that if your list of people |
|---|
| 624 |
was not sorted by gender, you'd need to make sure it is sorted before using it, |
|---|
| 625 |
i.e.:: |
|---|
| 626 |
|
|---|
| 627 |
{% regroup people|dictsort:"gender" by gender as grouped %} |
|---|
| 628 |
|
|---|
| 629 |
ssi |
|---|
| 630 |
~~~ |
|---|
| 631 |
|
|---|
| 632 |
Output the contents of a given file into the page. |
|---|
| 633 |
|
|---|
| 634 |
Like a simple "include" tag, ``{% ssi %}`` includes the contents of another |
|---|
| 635 |
file -- which must be specified using an absolute path -- in the current |
|---|
| 636 |
page:: |
|---|
| 637 |
|
|---|
| 638 |
{% ssi /home/html/ljworld.com/includes/right_generic.html %} |
|---|
| 639 |
|
|---|
| 640 |
If the optional "parsed" parameter is given, the contents of the included |
|---|
| 641 |
file are evaluated as template code, within the current context:: |
|---|
| 642 |
|
|---|
| 643 |
{% ssi /home/html/ljworld.com/includes/right_generic.html parsed %} |
|---|
| 644 |
|
|---|
| 645 |
Note that if you use ``{% ssi %}``, you'll need to define |
|---|
| 646 |
`ALLOWED_INCLUDE_ROOTS`_ in your Django settings, as a security measure. |
|---|
| 647 |
|
|---|
| 648 |
.. _ALLOWED_INCLUDE_ROOTS: http://www.djangoproject.com/documentation/settings/#allowed-include-roots |
|---|
| 649 |
|
|---|
| 650 |
templatetag |
|---|
| 651 |
~~~~~~~~~~~ |
|---|
| 652 |
|
|---|
| 653 |
Output one of the syntax characters used to compose template tags. |
|---|
| 654 |
|
|---|
| 655 |
Since the template system has no concept of "escaping", to display one of the |
|---|
| 656 |
bits used in template tags, you must use the ``{% templatetag %}`` tag. |
|---|
| 657 |
|
|---|
| 658 |
The argument tells which template bit to output: |
|---|
| 659 |
|
|---|
| 660 |
================== ======= |
|---|
| 661 |
Argument Outputs |
|---|
| 662 |
================== ======= |
|---|
| 663 |
``openblock`` ``{%`` |
|---|
| 664 |
``closeblock`` ``%}`` |
|---|
| 665 |
``openvariable`` ``{{`` |
|---|
| 666 |
``closevariable`` ``}}`` |
|---|
| 667 |
================== ======= |
|---|
| 668 |
|
|---|
| 669 |
widthratio |
|---|
| 670 |
~~~~~~~~~~ |
|---|
| 671 |
|
|---|
| 672 |
For creating bar charts and such, this tag calculates the ratio of a given value |
|---|
| 673 |
to a maximum value, and then applies that ratio to a constant. |
|---|
| 674 |
|
|---|
| 675 |
For example:: |
|---|
| 676 |
|
|---|
| 677 |
<img src='bar.gif' height='10' width='{% widthratio this_value max_value 100 %}' /> |
|---|
| 678 |
|
|---|
| 679 |
Above, if ``this_value`` is 175 and ``max_value`` is 200, the the image in the |
|---|
| 680 |
above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5 |
|---|
| 681 |
which is rounded up to 88). |
|---|
| 682 |
|
|---|
| 683 |
Built-in filter reference |
|---|
| 684 |
------------------------- |
|---|
| 685 |
|
|---|
| 686 |
add |
|---|
| 687 |
~~~ |
|---|
| 688 |
|
|---|
| 689 |
Adds the arg to the value. |
|---|
| 690 |
|
|---|
| 691 |
addslashes |
|---|
| 692 |
~~~~~~~~~~ |
|---|
| 693 |
|
|---|
| 694 |
Adds slashes. Useful for passing strings to JavaScript, for example. |
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 |
capfirst |
|---|
| 698 |
~~~~~~~~ |
|---|
| 699 |
|
|---|
| 700 |
Capitalizes the first character of the value. |
|---|
| 701 |
|
|---|
| 702 |
center |
|---|
| 703 |
~~~~~~ |
|---|
| 704 |
|
|---|
| 705 |
Centers the value in a field of a given width. |
|---|
| 706 |
|
|---|
| 707 |
cut |
|---|
| 708 |
~~~ |
|---|
| 709 |
|
|---|
| 710 |
Removes all values of arg from the given string. |
|---|
| 711 |
|
|---|
| 712 |
date |
|---|
| 713 |
~~~~ |
|---|
| 714 |
|
|---|
| 715 |
Formats a date according to the given format (same as the ``now`` tag). |
|---|
| 716 |
|
|---|
| 717 |
default |
|---|
| 718 |
~~~~~~~ |
|---|
| 719 |
|
|---|
| 720 |
If value is unavailable, use given default. |
|---|
| 721 |
|
|---|
| 722 |
default_if_none |
|---|
| 723 |
~~~~~~~~~~~~~~~ |
|---|
| 724 |
|
|---|
| 725 |
If value is ``None``, use given default. |
|---|
| 726 |
|
|---|
| 727 |
dictsort |
|---|
| 728 |
~~~~~~~~ |
|---|
| 729 |
|
|---|
| 730 |
Takes a list of dicts, returns that list sorted by the property given in the |
|---|
| 731 |
argument. |
|---|
| 732 |
|
|---|
| 733 |
dictsortreversed |
|---|
| 734 |
~~~~~~~~~~~~~~~~ |
|---|
| 735 |
|
|---|
| 736 |
Takes a list of dicts, returns that list sorted in reverse order by the |
|---|
| 737 |
property given in the argument. |
|---|
| 738 |
|
|---|
| 739 |
divisibleby |
|---|
| 740 |
~~~~~~~~~~~ |
|---|
| 741 |
|
|---|
| 742 |
Returns true if the value is divisible by the argument. |
|---|
| 743 |
|
|---|
| 744 |
escape |
|---|
| 745 |
~~~~~~ |
|---|
| 746 |
|
|---|
| 747 |
Escapes a string's HTML. Specifically, it makes these replacements: |
|---|
| 748 |
|
|---|
| 749 |
* ``"&"`` to ``"&"`` |
|---|
| 750 |
* ``<`` to ``"<"`` |
|---|
| 751 |
* ``>`` to ``">"`` |
|---|
| 752 |
* ``'"'`` (double quote) to ``"""`` |
|---|
| 753 |
|
|---|
| 754 |
filesizeformat |
|---|
| 755 |
~~~~~~~~~~~~~~ |
|---|
| 756 |
|
|---|
| 757 |
Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102 |
|---|
| 758 |
bytes, etc). |
|---|
| 759 |
|
|---|
| 760 |
first |
|---|
| 761 |
~~~~~ |
|---|
| 762 |
|
|---|
| 763 |
Returns the first item in a list. |
|---|
| 764 |
|
|---|
| 765 |
fix_ampersands |
|---|
| 766 |
~~~~~~~~~~~~~~ |
|---|
| 767 |
|
|---|
| 768 |
Replaces ampersands with ``&`` entities. |
|---|
| 769 |
|
|---|
| 770 |
floatformat |
|---|
| 771 |
|
|---|
| 772 |
Displays a floating point number as 34.2 (with one decimal places) -- but only |
|---|
| 773 |
if there's a point to be displayed. |
|---|
| 774 |
|
|---|
| 775 |
get_digit |
|---|
| 776 |
~~~~~~~~~ |
|---|
| 777 |
|
|---|
| 778 |
Given a whole number, returns the requested digit of it, where 1 is the |
|---|
| 779 |
right-most digit, 2 is the second-right-most digit, etc. Returns the original |
|---|
| 780 |
value for invalid input (if input or argument is not an integer, or if argument |
|---|
| 781 |
is less than 1). Otherwise, output is always an integer. |
|---|
| 782 |
|
|---|
| 783 |
join |
|---|
| 784 |
~~~~ |
|---|
| 785 |
|
|---|
| 786 |
Joins a list with a string, like Python's ``str.join(list)``. |
|---|
| 787 |
|
|---|
| 788 |
length |
|---|
| 789 |
~~~~~~ |
|---|
| 790 |
|
|---|
| 791 |
Returns the length of the value. Useful for lists. |
|---|
| 792 |
|
|---|
| 793 |
length_is |
|---|
| 794 |
~~~~~~~~~ |
|---|
| 795 |
|
|---|
| 796 |
Returns a boolean of whether the value's length is the argument. |
|---|
| 797 |
|
|---|
| 798 |
linebreaks |
|---|
| 799 |
~~~~~~~~~~ |
|---|
| 800 |
|
|---|
| 801 |
Converts newlines into <p> and <br />s. |
|---|
| 802 |
|
|---|
| 803 |
linebreaksbr |
|---|
| 804 |
~~~~~~~~~~~~ |
|---|
| 805 |
|
|---|
| 806 |
Converts newlines into <br />s. |
|---|
| 807 |
|
|---|
| 808 |
linenumbers |
|---|
| 809 |
~~~~~~~~~~~ |
|---|
| 810 |
|
|---|
| 811 |
Displays text with line numbers. |
|---|
| 812 |
|
|---|
| 813 |
ljust |
|---|
| 814 |
~~~~~ |
|---|
| 815 |
|
|---|
| 816 |
Left-aligns the value in a field of a given width. |
|---|
| 817 |
|
|---|
| 818 |
**Argument:** field size |
|---|
| 819 |
|
|---|
| 820 |
lower |
|---|
| 821 |
~~~~~ |
|---|
| 822 |
|
|---|
| 823 |
Converts a string into all lowercase. |
|---|
| 824 |
|
|---|
| 825 |
make_list |
|---|
| 826 |
~~~~~~~~~ |
|---|
| 827 |
|
|---|
| 828 |
Returns the value turned into a list. For an integer, it's a list of |
|---|
| 829 |
digits. For a string, it's a list of characters. |
|---|
| 830 |
|
|---|
| 831 |
phone2numeric |
|---|
| 832 |
~~~~~~~~~~~~~ |
|---|
| 833 |
|
|---|
| 834 |
Converts a phone number to its numerical equivalent. |
|---|
| 835 |
|
|---|
| 836 |
pluralize |
|---|
| 837 |
~~~~~~~~~ |
|---|
| 838 |
|
|---|
| 839 |
Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'. |
|---|
| 840 |
|
|---|
| 841 |
pprint |
|---|
| 842 |
~~~~~~ |
|---|
| 843 |
|
|---|
| 844 |
A wrapper around pprint.pprint -- for debugging, really. |
|---|
| 845 |
|
|---|
| 846 |
random |
|---|
| 847 |
~~~~~~ |
|---|
| 848 |
|
|---|
| 849 |
Returns a random item from the list. |
|---|
| 850 |
|
|---|
| 851 |
removetags |
|---|
| 852 |
~~~~~~~~~~ |
|---|
| 853 |
|
|---|
| 854 |
Removes a space separated list of [X]HTML tags from the output. |
|---|
| 855 |
|
|---|
| 856 |
rjust |
|---|
| 857 |
~~~~~ |
|---|
| 858 |
|
|---|
| 859 |
Right-aligns the value in a field of a given width. |
|---|
| 860 |
|
|---|
| 861 |
**Argument:** field size |
|---|
| 862 |
|
|---|
| 863 |
slice |
|---|
| 864 |
~~~~~ |
|---|
| 865 |
|
|---|
| 866 |
Returns a slice of the list. |
|---|
| 867 |
|
|---|
| 868 |
Uses the same syntax as Python's list slicing. See |
|---|
| 869 |
http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice |
|---|
| 870 |
for an introduction. |
|---|
| 871 |
|
|---|
| 872 |
Example: ``{{ some_list|slice:":2" }}`` |
|---|
| 873 |
|
|---|
| 874 |
slugify |
|---|
| 875 |
~~~~~~~ |
|---|
| 876 |
|
|---|
| 877 |
Converts to lowercase, removes non-word characters (alphanumerics and |
|---|
| 878 |
underscores) and converts spaces to hyphens. Also strips leading and trailing |
|---|
| 879 |
whitespace. |
|---|
| 880 |
|
|---|
| 881 |
stringformat |
|---|
| 882 |
~~~~~~~~~~~~ |
|---|
| 883 |
|
|---|
| 884 |
Formats the variable according to the argument, a string formatting specifier. |
|---|
| 885 |
This specifier uses Python string formating syntax, with the exception that |
|---|
| 886 |
the leading "%" is dropped. |
|---|
| 887 |
|
|---|
| 888 |
See http://docs.python.org/lib/typesseq-strings.html for documentation of |
|---|
| 889 |
Python string formatting |
|---|
| 890 |
|
|---|
| 891 |
striptags |
|---|
| 892 |
~~~~~~~~~ |
|---|
| 893 |
|
|---|
| 894 |
Strips all [X]HTML tags. |
|---|
| 895 |
|
|---|
| 896 |
time |
|---|
| 897 |
~~~~ |
|---|
| 898 |
|
|---|
| 899 |
Formats a time according to the given format (same as the ``now`` tag). |
|---|
| 900 |
|
|---|
| 901 |
timesince |
|---|
| 902 |
~~~~~~~~~ |
|---|
| 903 |
|
|---|
| 904 |
Formats a date as the time since that date (i.e. "4 days, 6 hours"). |
|---|
| 905 |
|
|---|
| 906 |
title |
|---|
| 907 |
~~~~~ |
|---|
| 908 |
|
|---|
| 909 |
Converts a string into titlecase. |
|---|
| 910 |
|
|---|
| 911 |
truncatewords |
|---|
| 912 |
~~~~~~~~~~~~~ |
|---|
| 913 |
|
|---|
| 914 |
Truncates a string after a certain number of words. |
|---|
| 915 |
|
|---|
| 916 |
**Argument:** Number of words to truncate after |
|---|
| 917 |
|
|---|
| 918 |
unordered_list |
|---|
| 919 |
~~~~~~~~~~~~~~ |
|---|
| 920 |
|
|---|
| 921 |
Recursively takes a self-nested list and returns an HTML unordered list -- |
|---|
| 922 |
WITHOUT opening and closing <ul> tags. |
|---|
| 923 |
|
|---|
| 924 |
The list is assumed to be in the proper format. For example, if ``var`` contains |
|---|
| 925 |
``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``, |
|---|
| 926 |
then ``{{ var|unordered_list }}`` would return:: |
|---|
| 927 |
|
|---|
| 928 |
<li>States |
|---|
| 929 |
<ul> |
|---|
| 930 |
<li>Kansas |
|---|
| 931 |
<ul> |
|---|
| 932 |
<li>Lawrence</li> |
|---|
| 933 |
<li>Topeka</li> |
|---|
| 934 |
</ul> |
|---|
| 935 |
</li> |
|---|
| 936 |
<li>Illinois</li> |
|---|
| 937 |
</ul> |
|---|
| 938 |
</li> |
|---|
| 939 |
|
|---|
| 940 |
upper |
|---|
| 941 |
~~~~~ |
|---|
| 942 |
|
|---|
| 943 |
Converts a string into all uppercase. |
|---|
| 944 |
|
|---|
| 945 |
urlencode |
|---|
| 946 |
~~~~~~~~~ |
|---|
| 947 |
|
|---|
| 948 |
Escapes a value for use in a URL. |
|---|
| 949 |
|
|---|
| 950 |
urlize |
|---|
| 951 |
~~~~~~ |
|---|
| 952 |
|
|---|
| 953 |
Converts URLs in plain text into clickable links. |
|---|
| 954 |
|
|---|
| 955 |
urlizetrunc |
|---|
| 956 |
~~~~~~~~~~~ |
|---|
| 957 |
|
|---|
| 958 |
Converts URLs into clickable links, truncating URLs to the given character limit. |
|---|
| 959 |
|
|---|
| 960 |
**Argument:** Length to truncate URLs to |
|---|
| 961 |
|
|---|
| 962 |
wordcount |
|---|
| 963 |
~~~~~~~~~ |
|---|
| 964 |
|
|---|
| 965 |
Returns the number of words. |
|---|
| 966 |
|
|---|
| 967 |
wordwrap |
|---|
| 968 |
~~~~~~~~ |
|---|
| 969 |
|
|---|
| 970 |
Wraps words at specified line length. |
|---|
| 971 |
|
|---|
| 972 |
**Argument:** number of words at which to wrap the text |
|---|
| 973 |
|
|---|
| 974 |
yesno |
|---|
| 975 |
~~~~~ |
|---|
| 976 |
|
|---|
| 977 |
Given a string mapping values for true, false and (optionally) None, |
|---|
| 978 |
returns one of those strings according to the value: |
|---|
| 979 |
|
|---|
| 980 |
========== ====================== ================================== |
|---|
| 981 |
Value Argument Outputs |
|---|
| 982 |
========== ====================== ================================== |
|---|
| 983 |
``True`` ``"yeah,no,maybe"`` ``yeah`` |
|---|
| 984 |
``False`` ``"yeah,no,maybe"`` ``no`` |
|---|
| 985 |
``None`` ``"yeah,no,maybe"`` ``maybe`` |
|---|
| 986 |
``None`` ``"yeah,no"`` ``"no"`` (converts None to False |
|---|
| 987 |
if no mapping for None is given) |
|---|
| 988 |
========== ====================== ================================== |
|---|