mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
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
This commit is contained in:
parent
136c4f8549
commit
4aa97f5c18
3 changed files with 75 additions and 5 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue