Ticket #6295: forloop.diff

File forloop.diff, 3.0 KB (added by Ned Batchelder, 17 years ago)

the patch!

  • django/template/defaulttags.py

     
    125125        if self.is_reversed:
    126126            values = reversed(values)
    127127        unpack = len(self.loopvars) > 1
     128        # Create a forloop value in the context.  We'll update counters on each
     129        # iteration just below.
     130        loop_dict = context['forloop'] = {'parentloop': parentloop}
    128131        for i, item in enumerate(values):
    129             context['forloop'] = {
    130                 # Shortcuts for current loop iteration number.
    131                 'counter0': i,
    132                 'counter': i+1,
    133                 # Reverse counter iteration numbers.
    134                 'revcounter': len_values - i,
    135                 'revcounter0': len_values - i - 1,
    136                 # Boolean values designating first and last times through loop.
    137                 'first': (i == 0),
    138                 'last': (i == len_values - 1),
    139                 'parentloop': parentloop,
    140             }
     132            # Shortcuts for current loop iteration number.
     133            loop_dict['counter0'] = i
     134            loop_dict['counter'] = i+1
     135            # Reverse counter iteration numbers.
     136            loop_dict['revcounter'] = len_values - i
     137            loop_dict['revcounter0'] = len_values - i - 1
     138            # Boolean values designating first and last times through loop.
     139            loop_dict['first'] = (i == 0)
     140            loop_dict['last'] = (i == len_values - 1)
     141
    141142            if unpack:
    142143                # If there are multiple loop variables, unpack the item into
    143144                # them.
  • tests/regressiontests/templates/tests.py

     
    441441            'for-tag-vars02': ("{% for val in values %}{{ forloop.counter0 }}{% endfor %}", {"values": [6, 6, 6]}, "012"),
    442442            'for-tag-vars03': ("{% for val in values %}{{ forloop.revcounter }}{% endfor %}", {"values": [6, 6, 6]}, "321"),
    443443            'for-tag-vars04': ("{% for val in values %}{{ forloop.revcounter0 }}{% endfor %}", {"values": [6, 6, 6]}, "210"),
     444            'for-tag-vars05': ("{% for val in values %}{% if forloop.first %}f{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "fxx"),
     445            'for-tag-vars06': ("{% for val in values %}{% if forloop.last %}l{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "xxl"),
    444446            'for-tag-unpack01': ("{% for key,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
    445447            'for-tag-unpack03': ("{% for key, value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
    446448            'for-tag-unpack04': ("{% for key , value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),
Back to Top