summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-11-24 22:01:48 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-11-24 22:01:48 +0000
commit4aa97f5c18f1242d4ef33931cf7567f997f24a6f (patch)
treeacb2d4e98e3711598ee78db8f0048a9d0f285c49 /docs
parent136c4f85490e058e9072206252807c831965a867 (diff)
Fixed #6398: added an optional `{% empty %}` clause to the `{% for %}` template tag. The contents of this clause are rendered if the list iterated over turns out to be empty. Thanks, Jannis Leidel.
Astute readers will notice that the patch originally called this `default`; after consideration I decided that `empty` is a very slightly better color for this particular bikeshed. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9530 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/templates/builtins.txt29
1 files changed, 29 insertions, 0 deletions
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index a14f50adf0..f67f1a86cd 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -234,6 +234,35 @@ The for loop sets a number of variables available within the loop:
current one
========================== ================================================
+for ... empty
+^^^^^^^^^^^^^
+
+.. versionadded:: 1.1
+
+The ``for`` tag can take an optional ``{% empty %}`` clause that will be
+displayed if the given array is empty or could not be found::
+
+ <ul>
+ {% for athlete in athlete_list %}
+ <li>{{ athlete.name }}</li>
+ {% empty %}
+ <li>Sorry, no athlete in this list!</li>
+ {% endfor %}
+ <ul>
+
+The above is equivalent to -- but shorter, cleaner, and possibly faster
+than -- the following::
+
+ <ul>
+ {% if althete_list %}
+ {% for athlete in athlete_list %}
+ <li>{{ athlete.name }}</li>
+ {% endfor %}
+ {% else %}
+ <li>Sorry, no athletes in this list.</li>
+ {% endif %}
+ </ul>
+
.. templatetag:: if
if