diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-11-24 22:01:48 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-11-24 22:01:48 +0000 |
| commit | 4aa97f5c18f1242d4ef33931cf7567f997f24a6f (patch) | |
| tree | acb2d4e98e3711598ee78db8f0048a9d0f285c49 | |
| parent | 136c4f85490e058e9072206252807c831965a867 (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
| -rw-r--r-- | django/template/defaulttags.py | 48 | ||||
| -rw-r--r-- | docs/ref/templates/builtins.txt | 29 | ||||
| -rw-r--r-- | tests/regressiontests/templates/tests.py | 3 |
3 files changed, 75 insertions, 5 deletions
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 8182cd4d8c..06ad5c635a 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -83,10 +83,14 @@ class FirstOfNode(Node): return u'' class ForNode(Node): - def __init__(self, loopvars, sequence, is_reversed, nodelist_loop): + def __init__(self, loopvars, sequence, is_reversed, nodelist_loop, nodelist_empty=None): self.loopvars, self.sequence = loopvars, sequence self.is_reversed = is_reversed self.nodelist_loop = nodelist_loop + if nodelist_empty is None: + self.nodelist_empty = NodeList() + else: + self.nodelist_empty = nodelist_empty def __repr__(self): reversed_text = self.is_reversed and ' reversed' or '' @@ -97,16 +101,18 @@ class ForNode(Node): def __iter__(self): for node in self.nodelist_loop: yield node + for node in self.nodelist_empty: + yield node def get_nodes_by_type(self, nodetype): nodes = [] if isinstance(self, nodetype): nodes.append(self) nodes.extend(self.nodelist_loop.get_nodes_by_type(nodetype)) + nodes.extend(self.nodelist_empty.get_nodes_by_type(nodetype)) return nodes def render(self, context): - nodelist = NodeList() if 'forloop' in context: parentloop = context['forloop'] else: @@ -121,6 +127,9 @@ class ForNode(Node): if not hasattr(values, '__len__'): values = list(values) len_values = len(values) + if len_values < 1: + return self.nodelist_empty.render(context) + nodelist = NodeList() if self.is_reversed: values = reversed(values) unpack = len(self.loopvars) > 1 @@ -610,6 +619,30 @@ def do_for(parser, token): {{ key }}: {{ value }} {% endfor %} + 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 athletes 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> + The for loop sets a number of variables available within the loop: ========================== ================================================ @@ -646,9 +679,14 @@ def do_for(parser, token): " %s" % token.contents) sequence = parser.compile_filter(bits[in_index+1]) - nodelist_loop = parser.parse(('endfor',)) - parser.delete_first_token() - return ForNode(loopvars, sequence, is_reversed, nodelist_loop) + nodelist_loop = parser.parse(('default', 'endfor',)) + token = parser.next_token() + if token.contents == 'default': + nodelist_default = parser.parse(('endfor',)) + parser.delete_first_token() + else: + nodelist_default = None + return ForNode(loopvars, sequence, is_reversed, nodelist_loop, nodelist_default) do_for = register.tag("for", do_for) def do_ifequal(parser, token, negate): 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 diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index cdf56ab56c..6e29666454 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -484,6 +484,9 @@ class Templates(unittest.TestCase): 'for-tag-unpack11': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, ("one:1,/two:2,/", "one:1,INVALID/two:2,INVALID/")), 'for-tag-unpack12': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2))}, ("one:1,carrot/two:2,/", "one:1,carrot/two:2,INVALID/")), 'for-tag-unpack13': ("{% for x,y,z in items %}{{ x }}:{{ y }},{{ z }}/{% endfor %}", {"items": (('one', 1, 'carrot'), ('two', 2, 'cheese'))}, ("one:1,carrot/two:2,cheese/", "one:1,carrot/two:2,cheese/")), + 'for-tag-default01': ("{% for val in values %}{{ val }}{% default %}default text{% endfor %}", {"values": [1, 2, 3]}, "123"), + 'for-tag-default02': ("{% for val in values %}{{ val }}{% default %}values array empty{% endfor %}", {"values": []}, "values array empty"), + 'for-tag-default03': ("{% for val in values %}{{ val }}{% default %}values array not found{% endfor %}", {}, "values array not found"), ### IF TAG ################################################################ 'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"), |
