2005-09-05 23:38:46 +00:00
|
|
|
==================================================
|
|
|
|
The Django template language: For template authors
|
|
|
|
==================================================
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
Django's template language is designed to strike a balance between power and
|
2005-08-09 22:39:25 +00:00
|
|
|
ease. It's designed to feel comfortable to those used to working with HTML. If
|
|
|
|
you have any exposure to other text-based template languages, such as Smarty_
|
|
|
|
or CheetahTemplate_, you should feel right at home with Django's templates.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
.. _Smarty: http://smarty.php.net/
|
|
|
|
.. _CheetahTemplate: http://www.cheetahtemplate.org/
|
|
|
|
|
|
|
|
What's a template?
|
|
|
|
==================
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
A template is simply a text file. All Django templates, by convention, have
|
|
|
|
".html" extensions, but they can generate any text-based format (HTML, XML,
|
|
|
|
CSV, etc.).
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
A template contains **variables**, which get replaced with values when the
|
|
|
|
template is evaluated, and **tags**, which control the logic of the template.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Below is a minimal template that illustrates a few basics. Each element will be
|
|
|
|
explained later in this document.::
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
{% extends "base_generic" %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
{% block title %}{{ section.title }}{% endblock %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
{% block content %}
|
|
|
|
<h1>{{ section.title }}</h1>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
{% for story in story_list %}
|
|
|
|
<h2>
|
|
|
|
<a href="{{ story.get_absolute_url }}">
|
|
|
|
{{ story.headline|upper }}
|
|
|
|
</a>
|
|
|
|
</h2>
|
|
|
|
<p>{{ story.tease|truncatewords:"100" }}</p>
|
|
|
|
{% endfor %}
|
|
|
|
{% endblock %}
|
2005-08-09 21:42:27 +00:00
|
|
|
|
2005-07-21 19:05:10 +00:00
|
|
|
.. admonition:: Philosophy
|
|
|
|
|
2005-08-09 21:42:27 +00:00
|
|
|
Why use a text-based template instead of an XML-based one (like Zope's
|
2005-08-09 22:39:25 +00:00
|
|
|
TAL)? We wanted Django's template language to be usable for more than
|
|
|
|
just XML/HTML templates. At World Online, we use it for e-mails,
|
2005-09-05 23:38:46 +00:00
|
|
|
JavaScript and CSV. You can use the template language for any text-based
|
2005-07-21 19:05:10 +00:00
|
|
|
format.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
What's a variable?
|
|
|
|
==================
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Variables look like this: ``{{ variable }}``. When the template engine
|
|
|
|
encounters a variable, it evaluates that variable and replaces it with the
|
|
|
|
result.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Use a dot (``.``) to access attributes of a variable.
|
|
|
|
|
|
|
|
.. admonition:: Behind the scenes
|
|
|
|
|
|
|
|
Technically, when the template system encounters a dot, it tries the
|
|
|
|
following lookups, in this order:
|
|
|
|
|
|
|
|
* Dictionary lookup
|
|
|
|
* Attribute lookup
|
|
|
|
* Method call
|
|
|
|
* List-index lookup
|
|
|
|
|
|
|
|
In the above example, ``{{ section.title }}`` will be replaced with the
|
|
|
|
``title`` attribute of the ``section`` object.
|
|
|
|
|
|
|
|
If you use a variable that doesn't exist, it will be silently ignored. The
|
2005-07-13 01:25:57 +00:00
|
|
|
variable will be replaced by nothingness.
|
|
|
|
|
|
|
|
See `Using the built-in reference`_, below, for help on finding what variables
|
|
|
|
are available in a given template.
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
You can modify variables for display by using **filters**.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
What's a filter?
|
|
|
|
================
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Filters look like this: ``{{ name|lower }}``. This displays the value of the
|
|
|
|
``{{ name }}`` variable after being filtered through the ``lower`` filter,
|
|
|
|
which converts text to lowercase. Use a pipe (``|``) to apply a filter.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Filters can be "chained." The output of one filter applied to the next:
|
|
|
|
``{{ text|escape|linebreaks }}`` is a common idiom for escaping text contents
|
|
|
|
and then converting line breaks to ``<p>`` tags.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Certain filters take arguments. A filter argument looks like this:
|
|
|
|
``{{ bio|truncatewords:"30" }}``. This will display the first 30 words of the
|
|
|
|
``bio`` variable. Filter arguments always are in double quotes.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
The `Built-in filter reference`_ below describes all the built-in filters.
|
|
|
|
|
|
|
|
What's a tag?
|
|
|
|
=============
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Tags look like this: ``{% tag %}``. Tags are more complex than variables: Some
|
|
|
|
create text in the output, some control flow by performing loops or logic, and
|
2005-07-13 01:25:57 +00:00
|
|
|
some load external information into the template to be used by later variables.
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Some tags require beginning and ending tags (i.e.
|
|
|
|
``{% tag %} ... tag contents ... {% endtag %}``). The `Built-in tag reference`_
|
|
|
|
below describes all the built-in tags. You can create your own tags, if you
|
|
|
|
know how to write Python code.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Template inheritance
|
2005-07-13 01:25:57 +00:00
|
|
|
====================
|
|
|
|
|
|
|
|
The most powerful -- and thus the most complex -- part of Django's template
|
2005-08-09 22:39:25 +00:00
|
|
|
engine is template inheritance. Template inheritance allows you to build a base
|
|
|
|
"skeleton" template that contains all the common elements of your site and
|
|
|
|
defines **blocks** that child templates can override.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
It's easiest to understand template inheritance by starting with an example::
|
|
|
|
|
2005-07-16 17:13:20 +00:00
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
2005-07-13 01:25:57 +00:00
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" href="style.css" />
|
2005-08-09 22:39:25 +00:00
|
|
|
<title>{% block title %}My amazing site{% endblock %}</title>
|
2005-07-13 01:25:57 +00:00
|
|
|
</head>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
<body>
|
|
|
|
<div id="sidebar">
|
|
|
|
{% block sidebar %}
|
|
|
|
<ul>
|
|
|
|
<li><a href="/">Home</a></li>
|
|
|
|
<li><a href="/blog/">Blog</a></li>
|
|
|
|
</ul>
|
|
|
|
{% endblock %}
|
|
|
|
</div>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
<div id="content">
|
|
|
|
{% block content %}{% endblock %}
|
|
|
|
</div>
|
|
|
|
</body>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
This template, which we'll call ``base.html``, defines a simple HTML skeleton
|
|
|
|
document that you might use for a simple two-column page. It's the job of
|
|
|
|
"child" templates to fill the empty blocks with content.
|
2005-07-15 21:44:19 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
In this example, the ``{% block %}`` tag defines three blocks that child
|
|
|
|
templates can fill in. All the ``block`` tag does is to tell the template
|
|
|
|
engine that a child template may override those portions of the template.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
A child template might look like this::
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
{% extends "base" %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
{% block title %}My amazing blog{% endblock %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
{% block content %}
|
|
|
|
{% for entry in blog_entries %}
|
|
|
|
<h2>{{ entry.title }}</h2>
|
|
|
|
<p>{{ entry.body }}</p>
|
|
|
|
{% endfor %}
|
|
|
|
{% endblock %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
The ``{% extends %}`` tag is the key here. It tells the template engine that
|
|
|
|
this template "extends" another template. When the template system evaluates
|
|
|
|
this template, first it locates the parent -- in this case, "base" (note the
|
|
|
|
lack of an ".html" extension in the ``{% extends %}`` tag).
|
2005-07-15 21:44:19 +00:00
|
|
|
|
|
|
|
At that point, the template engine will notice the three blocks in
|
2005-08-09 22:39:25 +00:00
|
|
|
``base.html`` and replace those blocks with the contents of the child template.
|
2005-07-15 21:44:19 +00:00
|
|
|
Depending on the value of ``blog_entries``, the output might look like::
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-07-16 17:13:20 +00:00
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
2005-07-13 01:25:57 +00:00
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" href="style.css" />
|
2005-08-09 22:39:25 +00:00
|
|
|
<title>My amazing blog</title>
|
2005-07-13 01:25:57 +00:00
|
|
|
</head>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
<body>
|
|
|
|
<div id="sidebar">
|
|
|
|
<ul>
|
|
|
|
<li><a href="/">Home</a></li>
|
|
|
|
<li><a href="/blog/">Blog</a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
<div id="content">
|
|
|
|
<h2>Entry one</h2>
|
|
|
|
<p>This is my first entry.</p>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
<h2>Entry two</h2>
|
|
|
|
<p>This is my second entry.</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Note that since the child template didn't define the ``sidebar`` block, the
|
|
|
|
value from the parent template is used instead. Content within a ``{% block %}``
|
|
|
|
tag in a parent template is always used as a fallback.
|
2005-07-16 17:13:20 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Template inheritance isn't limited to a single level. Multi-level inheritance
|
|
|
|
is possible and, indeed, quite useful.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
Here are some tips for working with inheritance:
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
* More ``{% block %}`` tags in your base templates are better. Remember,
|
|
|
|
child templates don't have to define all parent blocks, so you can fill
|
|
|
|
in reasonable defaults in a number of blocks, then only define the ones
|
|
|
|
you need later.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
* If you find yourself duplicating content in a number of templates, it
|
|
|
|
probably means you should move that content to a ``{% block %}`` in a
|
|
|
|
parent template.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
* The recommended template layout is to use three levels: a single base
|
|
|
|
template for the entire site, a set of mid-level templates for each
|
|
|
|
section of the site, and then the individual templates for each view.
|
|
|
|
This maximizes code reuse and makes it easier to add items to shared
|
|
|
|
content areas (such as section-wide navigation).
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
* If you need to get the content of the block from the parent template,
|
2005-08-09 22:39:25 +00:00
|
|
|
the ``{{ block.super }}`` variable will do the trick. This is useful
|
2005-07-17 15:15:26 +00:00
|
|
|
if you want to add to the contents of a parent block instead of
|
2005-07-13 01:25:57 +00:00
|
|
|
completely overriding it.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
Using the built-in reference
|
|
|
|
============================
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Because Django can be used to develop any sort of site, the tags, filters and
|
|
|
|
variables available are different depending on the application. To make it
|
|
|
|
easy to figure out what's available in a given site, the admin interface has a
|
2005-07-15 21:44:19 +00:00
|
|
|
complete reference of all the template goodies available to that site.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
The reference is integrated into the administration interface for your site(s)
|
|
|
|
and is divided into 4 sections: tags, filters, models, and views.
|
2005-07-15 21:44:19 +00:00
|
|
|
|
|
|
|
The **tags** and **filters** sections describe all the built-in tags (in fact,
|
|
|
|
the tag and filter references below come directly from those pages) as well as
|
|
|
|
any custom tag or filter libraries available.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
The **views** page is the most valuable. Each URL in your site has a separate
|
|
|
|
entry here, and clicking on a URL will show you:
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
* The name of the view function that generates that view.
|
|
|
|
* A short description of what the view does.
|
2005-07-15 21:44:19 +00:00
|
|
|
* The **context**, or a list of variables available in the view.
|
2005-07-13 01:25:57 +00:00
|
|
|
* The name of the template or templates that are used for that view.
|
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
Each view documentation page also has a bookmarklet that you can use to jump
|
|
|
|
from any page to the documentation page for that view.
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Because Django generally revolves around database objects, the **models**
|
|
|
|
section of the documentation page describes each type of object in the system
|
|
|
|
along with all the fields available on that object.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
Taken together, the documentation pages should tell you every tag, filter,
|
2005-07-13 01:25:57 +00:00
|
|
|
variable and object available to you in a given template.
|
|
|
|
|
|
|
|
Custom tag and filter libraries
|
|
|
|
===============================
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
Certain applications provide custom tag and filter libraries. To access them in
|
|
|
|
a template, use the ``{% load %}`` tag::
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
{% load comments %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
{% comment_form for blogs.entries entry.id with is_public yes %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-13 01:25:57 +00:00
|
|
|
In the above, the ``load`` tag loads the ``comments`` tag library, which then
|
2005-08-09 22:39:25 +00:00
|
|
|
makes the ``comment_form`` tag available for use. Consult the documentation
|
2005-07-13 01:25:57 +00:00
|
|
|
area in your admin to find the list of custom libraries in your installation.
|
|
|
|
|
|
|
|
Built-in tag and filter reference
|
|
|
|
=================================
|
|
|
|
|
2005-08-09 22:39:25 +00:00
|
|
|
For those without an admin site available, reference for the stock tags and
|
|
|
|
filters follows. Because Django is highly customizable, the reference in your
|
|
|
|
admin should be considered the final word on what tags and filters are
|
|
|
|
available, and what they do.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
Built-in tag reference
|
|
|
|
----------------------
|
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``block``
|
2005-08-09 22:39:25 +00:00
|
|
|
Define a block that can be overridden by child templates. See `Template
|
2005-07-15 00:42:28 +00:00
|
|
|
inheritance`_ for more information.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``comment``
|
|
|
|
Ignore everything between ``{% comment %}`` and ``{% endcomment %}``
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``cycle``
|
|
|
|
Cycle among the given strings each time this tag is encountered.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Within a loop, cycles among the given strings each time through
|
|
|
|
the loop::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% for o in some_list %}
|
|
|
|
<tr class="{% cycle row1,row2 %}">
|
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Outside of a loop, give the values a unique name the first time you call it,
|
|
|
|
then use that name each successive time through::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
<tr class="{% cycle row1,row2,row3 as rowcolors %}">...</tr>
|
|
|
|
<tr class="{% cycle rowcolors %}">...</tr>
|
|
|
|
<tr class="{% cycle rowcolors %}">...</tr>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
You can use any number of values, separated by commas. Make sure not to put
|
|
|
|
spaces between the values -- only commas.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``debug``
|
|
|
|
Output a whole load of debugging information, including the current context and
|
|
|
|
imported modules.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``extends``
|
|
|
|
Signal that this template extends a parent template.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
This tag may be used in two ways: ``{% extends "base" %}`` (with quotes) uses
|
|
|
|
the literal value "base" as the name of the parent template to extend, or ``{%
|
|
|
|
extends variable %}`` uses the value of ``variable`` as the name of the parent
|
|
|
|
template to extend.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
See `Template inheritance`_ for more information.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``filter``
|
2005-07-17 15:15:26 +00:00
|
|
|
Filter the contents of the variable through variable filters.
|
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Filters can also be piped through each other, and they can have arguments --
|
|
|
|
just like in variable syntax.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Sample usage::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% filter escape|lower %}
|
|
|
|
This text will be HTML-escaped, and will appear in all lowercase.
|
|
|
|
{% endfilter %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``firstof``
|
|
|
|
Outputs the first variable passed that is not False. Outputs nothing if all the
|
|
|
|
passed variables are False.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Sample usage::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% firstof var1 var2 var3 %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
This is equivalent to::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% if var1 %}
|
|
|
|
{{ var1 }}
|
|
|
|
{% else %}{% if var2 %}
|
|
|
|
{{ var2 }}
|
|
|
|
{% else %}{% if var3 %}
|
|
|
|
{{ var3 }}
|
|
|
|
{% endif %}{% endif %}{% endif %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
but obviously much cleaner!
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``for``
|
|
|
|
Loop over each item in an array. For example, to display a list of athletes
|
|
|
|
given ``athlete_list``::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
<ul>
|
|
|
|
{% for athlete in athlete_list %}
|
|
|
|
<li>{{ athlete.name }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
You can also loop over a list in reverse by using ``{% for obj in list reversed %}``.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
The for loop sets a number of variables available within the loop:
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
========================== ================================================
|
|
|
|
Variable Description
|
|
|
|
========================== ================================================
|
|
|
|
``forloop.counter`` The current iteration of the loop (1-indexed)
|
|
|
|
``forloop.counter0`` The current iteration of the loop (0-indexed)
|
2005-09-29 23:34:29 +00:00
|
|
|
``forloop.revcounter`` The number of iterations from the end of the
|
|
|
|
loop (1-indexed)
|
|
|
|
``forloop.revcounter0`` The number of iterations from the end of the
|
|
|
|
loop (0-indexed)
|
2005-07-15 00:42:28 +00:00
|
|
|
``forloop.first`` True if this is the first time through the loop
|
|
|
|
``forloop.last`` True if this is the last time through the loop
|
|
|
|
``forloop.parentloop`` For nested loops, this is the loop "above" the
|
|
|
|
current one
|
|
|
|
========================== ================================================
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``if``
|
|
|
|
The ``{% if %}`` tag evaluates a variable, and if that variable is "true" (i.e.
|
|
|
|
exists, is not empty, and is not a false boolean value) the contents of the
|
|
|
|
block are output::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% if athlete_list %}
|
|
|
|
Number of athletes: {{ athlete_list|count }}
|
|
|
|
{% else %}
|
|
|
|
No athletes.
|
2005-07-13 01:25:57 +00:00
|
|
|
{% endif %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
In the above, if ``athlete_list`` is not empty, the number of athletes will be
|
|
|
|
displayed by the ``{{ athlete_list|count }}`` variable.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
As you can see, the ``if`` tag can take an option ``{% else %}`` clause that
|
|
|
|
will be displayed if the test fails.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``if`` tags may use ``or`` or ``not`` to test a number of variables or to negate
|
|
|
|
a given variable::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% if not athlete_list %}
|
|
|
|
There are no athletes.
|
|
|
|
{% endif %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% if athlete_list or coach_list %}
|
|
|
|
There are some athletes or some coaches.
|
|
|
|
{% endif %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% if not athlete_list or coach_list %}
|
2005-07-17 15:15:26 +00:00
|
|
|
There are no athletes or there are some coaches (OK, so
|
|
|
|
writing English translations of boolean logic sounds
|
2005-07-15 00:42:28 +00:00
|
|
|
stupid; it's not my fault).
|
|
|
|
{% endif %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
|
|
|
For simplicity, ``if`` tags do not allow ``and`` clauses; use nested ``if``
|
2005-07-15 00:42:28 +00:00
|
|
|
tags instead::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% if athlete_list %}
|
|
|
|
{% if coach_list %}
|
|
|
|
Number of athletes: {{ athlete_list|count }}.
|
|
|
|
Number of coaches: {{ coach_list|count }}.
|
|
|
|
{% endif %}
|
|
|
|
{% endif %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``ifchanged``
|
|
|
|
Check if a value has changed from the last iteration of a loop.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
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::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
<h1>Archive for {{ year }}</h1>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% for date in days %}
|
|
|
|
{% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}
|
|
|
|
<a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a>
|
|
|
|
{% endfor %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-08-29 21:58:21 +00:00
|
|
|
``ifequal``
|
|
|
|
Output the contents of the block if the two arguments equal each other.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
2005-09-01 02:21:08 +00:00
|
|
|
{% ifequal user.id comment.user_id %}
|
2005-08-29 21:58:21 +00:00
|
|
|
...
|
|
|
|
{% endifequal %}
|
|
|
|
|
2005-09-01 02:21:08 +00:00
|
|
|
As in the ``{% if %}`` tag, an ``{% else %}`` clause is optional.
|
2005-08-29 21:58:21 +00:00
|
|
|
|
2005-09-01 02:21:08 +00:00
|
|
|
The arguments can be hard-coded strings, so the following is valid::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-09-01 02:21:08 +00:00
|
|
|
{% ifequal user.username "adrian" %}
|
2005-07-15 00:42:28 +00:00
|
|
|
...
|
2005-09-01 02:21:08 +00:00
|
|
|
{% endifequal %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-09-01 02:21:08 +00:00
|
|
|
``ifnotequal``
|
|
|
|
Just like ``ifequal``, except it tests that the two arguments are not equal.
|
2005-08-29 21:58:21 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``load``
|
|
|
|
Load a custom template tag set.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
See `Custom tag and filter libraries`_ for more information.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``now``
|
|
|
|
Display the date, formatted according to the given string.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Uses the same format as PHP's ``date()`` function; see http://php.net/date
|
|
|
|
for all the possible values.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Sample usage::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
It is {% now "jS F Y H:i" %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``regroup``
|
|
|
|
Regroup a list of alike objects by a common attribute.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
This complex tag is best illustrated by use of an example: say that ``people``
|
|
|
|
is a list of ``Person`` objects that have ``first_name``, ``last_name``, and
|
|
|
|
``gender`` attributes, and you'd like to display a list that looks like:
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
* Male:
|
|
|
|
* George Bush
|
|
|
|
* Bill Clinton
|
|
|
|
* Female:
|
|
|
|
* Margaret Thatcher
|
2005-08-09 22:39:25 +00:00
|
|
|
* Condoleezza Rice
|
2005-07-15 00:42:28 +00:00
|
|
|
* Unknown:
|
2005-08-09 21:42:27 +00:00
|
|
|
* Pat Smith
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
The following snippet of template code would accomplish this dubious task::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% regroup people by gender as grouped %}
|
2005-07-13 01:25:57 +00:00
|
|
|
<ul>
|
2005-07-15 00:42:28 +00:00
|
|
|
{% for group in grouped %}
|
|
|
|
<li>{{ group.grouper }}
|
|
|
|
<ul>
|
|
|
|
{% for item in group.list %}
|
|
|
|
<li>{{ item }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
{% endfor %}
|
2005-07-13 01:25:57 +00:00
|
|
|
</ul>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
As you can see, ``{% regroup %}`` populates a variable with a list of objects
|
|
|
|
with ``grouper`` and ``list`` attributes. ``grouper`` contains the item that
|
|
|
|
was grouped by; ``list`` contains the list of objects that share that
|
|
|
|
``grouper``. In this case, ``grouper`` would be ``Male``, ``Female`` and
|
|
|
|
``Unknown``, and ``list`` is the list of people with those genders.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Note that ``{% regroup %}`` does not work when the list to be grouped is not
|
|
|
|
sorted by the key you are grouping by! This means that if your list of people
|
|
|
|
was not sorted by gender, you'd need to make sure it is sorted before using it,
|
|
|
|
i.e.::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% regroup people|dictsort:"gender" by gender as grouped %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``ssi``
|
|
|
|
Output the contents of a given file into the page.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-09-01 02:21:08 +00:00
|
|
|
Like a simple "include" tag, ``{% ssi %}`` includes the contents of another
|
|
|
|
file -- which must be specified using an absolute path -- in the current
|
|
|
|
page::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% ssi /home/html/ljworld.com/includes/right_generic.html %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
If the optional "parsed" parameter is given, the contents of the included
|
2005-09-01 02:21:08 +00:00
|
|
|
file are evaluated as template code, within the current context::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
{% ssi /home/html/ljworld.com/includes/right_generic.html parsed %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``templatetag``
|
|
|
|
Output one of the bits used to compose template tags.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Since the template system has no concept of "escaping", to display one of the
|
|
|
|
bits used in template tags, you must use the ``{% templatetag %}`` tag.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
The argument tells which template bit to output:
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
================== =======
|
|
|
|
Argument Outputs
|
|
|
|
================== =======
|
|
|
|
``openblock`` ``{%``
|
|
|
|
``closeblock`` ``%}``
|
|
|
|
``openvariable`` ``{{``
|
|
|
|
``closevariable`` ``}}``
|
|
|
|
================== =======
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``widthratio``
|
|
|
|
For creating bar charts and such, this tag calculates the ratio of a given value
|
|
|
|
to a maximum value, and then applies that ratio to a constant.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
For example::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
<img src='bar.gif' height='10' width='{% widthratio this_value max_value 100 %}' />
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Above, if ``this_value`` is 175 and ``max_value`` is 200, the the image in the
|
|
|
|
above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5
|
|
|
|
which is rounded up to 88).
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
Built-in filter reference
|
|
|
|
-------------------------
|
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``add``
|
|
|
|
Adds the arg to the value
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``addslashes``
|
|
|
|
Adds slashes - useful for passing strings to JavaScript, for example.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``capfirst``
|
|
|
|
Capitalizes the first character of the value
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``center``
|
|
|
|
Centers the value in a field of a given width
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``cut``
|
|
|
|
Removes all values of arg from the given string
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``date``
|
2005-07-15 00:53:18 +00:00
|
|
|
Formats a date according to the given format (same as the ``now`` tag)
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``default``
|
|
|
|
If value is unavailable, use given default
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``dictsort``
|
|
|
|
Takes a list of dicts, returns that list sorted by the property given in the
|
|
|
|
argument.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``dictsortreversed``
|
|
|
|
Takes a list of dicts, returns that list sorted in reverse order by the property
|
|
|
|
given in the argument.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``divisibleby``
|
|
|
|
Returns true if the value is divisible by the argument
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``escape``
|
|
|
|
Escapes a string's HTML
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``filesizeformat``
|
|
|
|
Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102
|
|
|
|
bytes, etc).
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``first``
|
|
|
|
Returns the first item in a list
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``fix_ampersands``
|
|
|
|
Replaces ampersands with ``&`` entities
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``floatformat``
|
|
|
|
Displays a floating point number as 34.2 (with one decimal places) - but
|
|
|
|
only if there's a point to be displayed
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``get_digit``
|
|
|
|
Given a whole number, returns the requested digit of it, where 1 is the
|
|
|
|
right-most digit, 2 is the second-right-most digit, etc. Returns the
|
|
|
|
original value for invalid input (if input or argument is not an integer,
|
|
|
|
or if argument is less than 1). Otherwise, output is always an integer.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``join``
|
|
|
|
Joins a list with a string, like Python's ``str.join(list)``
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``length``
|
|
|
|
Returns the length of the value - useful for lists
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``length_is``
|
|
|
|
Returns a boolean of whether the value's length is the argument
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``linebreaks``
|
|
|
|
Converts newlines into <p> and <br />s
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``linebreaksbr``
|
|
|
|
Converts newlines into <br />s
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``linenumbers``
|
|
|
|
Displays text with line numbers
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``ljust``
|
|
|
|
Left-aligns the value in a field of a given width
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
**Argument:** field size
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``lower``
|
|
|
|
Converts a string into all lowercase
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``make_list``
|
|
|
|
Returns the value turned into a list. For an integer, it's a list of
|
|
|
|
digits. For a string, it's a list of characters.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``phone2numeric``
|
|
|
|
Takes a phone number and converts it in to its numerical equivalent
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``pluralize``
|
|
|
|
Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``pprint``
|
|
|
|
A wrapper around pprint.pprint -- for debugging, really
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``random``
|
|
|
|
Returns a random item from the list
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``removetags``
|
|
|
|
Removes a space separated list of [X]HTML tags from the output
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``rjust``
|
|
|
|
Right-aligns the value in a field of a given width
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
**Argument:** field size
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``slice``
|
|
|
|
Returns a slice of the list.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
Uses the same syntax as Python's list slicing; see
|
|
|
|
http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
|
|
|
|
for an introduction.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-09-24 17:39:20 +00:00
|
|
|
Example: ``{{ some_list|slice:":2" }}``
|
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``slugify``
|
2005-09-01 22:58:58 +00:00
|
|
|
Converts to lowercase, removes non-word characters (alphanumerics and
|
|
|
|
underscores) and converts spaces to hyphens. Also strips leading and
|
|
|
|
trailing whitespace.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``stringformat``
|
|
|
|
Formats the variable according to the argument, a string formatting specifier.
|
|
|
|
This specifier uses Python string formating syntax, with the exception that
|
|
|
|
the leading "%" is dropped.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
See http://docs.python.org/lib/typesseq-strings.html for documentation
|
|
|
|
of Python string formatting
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``striptags``
|
|
|
|
Strips all [X]HTML tags
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``time``
|
2005-07-15 00:53:18 +00:00
|
|
|
Formats a time according to the given format (same as the ``now`` tag).
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``timesince``
|
|
|
|
Formats a date as the time since that date (i.e. "4 days, 6 hours")
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``title``
|
|
|
|
Converts a string into titlecase
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``truncatewords``
|
|
|
|
Truncates a string after a certain number of words
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
**Argument:** Number of words to truncate after
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``unordered_list``
|
|
|
|
Recursively takes a self-nested list and returns an HTML unordered list --
|
|
|
|
WITHOUT opening and closing <ul> tags.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
The list is assumed to be in the proper format. For example, if ``var`` contains
|
|
|
|
``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
|
|
|
|
then ``{{ var|unordered_list }}`` would return::
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
<li>States
|
|
|
|
<ul>
|
|
|
|
<li>Kansas
|
|
|
|
<ul>
|
|
|
|
<li>Lawrence</li>
|
|
|
|
<li>Topeka</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li>Illinois</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``upper``
|
|
|
|
Converts a string into all uppercase
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``urlencode``
|
|
|
|
Escapes a value for use in a URL
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``urlize``
|
|
|
|
Converts URLs in plain text into clickable links
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``urlizetrunc``
|
|
|
|
Converts URLs into clickable links, truncating URLs to the given character limit
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
**Argument:** Length to truncate URLs to.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``wordcount``
|
|
|
|
Returns the number of words
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``wordwrap``
|
|
|
|
Wraps words at specified line length
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 21:44:19 +00:00
|
|
|
**Argument:** number of words to wrap the text at.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
``yesno``
|
|
|
|
Given a string mapping values for true, false and (optionally) None,
|
|
|
|
returns one of those strings according to the value:
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-07-15 00:42:28 +00:00
|
|
|
========== ====================== ==================================
|
|
|
|
Value Argument Outputs
|
|
|
|
========== ====================== ==================================
|
|
|
|
``True`` ``"yeah,no,maybe"`` ``yeah``
|
|
|
|
``False`` ``"yeah,no,maybe"`` ``no``
|
|
|
|
``None`` ``"yeah,no,maybe"`` ``maybe``
|
|
|
|
``None`` ``"yeah,no"`` ``"no"`` (converts None to False
|
2005-07-15 21:44:19 +00:00
|
|
|
if no mapping for None is given)
|
2005-07-15 00:42:28 +00:00
|
|
|
========== ====================== ==================================
|