Skip to content
Myr

Add changed() to loop context

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Adrian Moennich · committed by Adrian Moennich · parent 9bdb547 · Browse files · 5 files+38 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+38 added−0 removed

  • tests:+14 added−0 removed
  • other:+24 added−0 removed

5 files+38 added−0 removed

modified:docs/templates.rst+13 added
616 unmodified lines
617+-----------------------+---------------------------------------------------+618| `loop.nextitem` | The item from the following iteration of the loop.|619| | Undefined during the last iteration. |620+-----------------------+---------------------------------------------------+621| `loop.changed(*val)` | True if previously called with a different value |622| | (or not called at all). |623+-----------------------+---------------------------------------------------+624625Within a for-loop, it's possible to cycle among a list of strings/variables626each time through the loop by using the special `loop.cycle` helper::627
74 unmodified lines
702 The value will increase even more!703 {% endif %}704 {% endfor %}705706If you only care whether the value changed at all, using `changed` is even707easier::708709 {% for entry in entries %}710 {% if loop.changed(entry.category) %}711 <h2>{{ entry.category }}</h2>712 {% endif %}713 <p>{{ entry.message }}</p>714 {% endfor %}715716.. _if:717718If719~~
880 unmodified lines
modified:jinja2/runtime.py+8 added
358 unmodified lines
359 self._undefined = undefined360 self._recurse = recurse361 self.index0 = -1362 self.depth0 = depth0363 self._last_checked_value = missing364365 def cycle(self, *args):366 """Cycles among the arguments with the current loop index."""367 if not args:368 raise TypeError('no items for cycling given')369 return args[self.index0 % len(args)]370371 def changed(self, *value):372 """Checks whether the value has changed since the last call."""373 if self._last_checked_value != value:374 self._last_checked_value = value375 return True376 return False377378 first = property(lambda x: x.index0 == 0)379 last = property(lambda x: x._after is _last_iteration)380 index = property(lambda x: x.index0 + 1)381 revindex = property(lambda x: x.length - x.index0)
431 unmodified lines
modified:tests/test_async.py+7 added
310 unmodified lines
311 {%- endfor %}''')312 output = tmpl.render(seq=list(range(4)))313 assert output == 'x-0-1|0-1-2|1-2-3|2-3-x|'314315 def test_changed(self, test_env_async):316 tmpl = test_env_async.from_string('''{% for item in seq -%}317 {{ loop.changed(item) }},318 {%- endfor %}''')319 output = tmpl.render(seq=[None, None, 1, 2, 2, 3, 4, 4, 4])320 assert output == 'True,False,True,True,False,True,True,False,False,'321322 def test_scope(self, test_env_async):323 tmpl = test_env_async.from_string('{% for item in seq %}{% endfor %}{{ item }}')324 output = tmpl.render(seq=list(range(10)))325 assert not output
187 unmodified lines
modified:tests/test_core_tags.py+7 added
74 unmodified lines
75 {%- endfor %}''')76 output = tmpl.render(seq=list(range(4)))77 assert output == 'x-0-1|0-1-2|1-2-3|2-3-x|'7879 def test_changed(self, env):80 tmpl = env.from_string('''{% for item in seq -%}81 {{ loop.changed(item) }},82 {%- endfor %}''')83 output = tmpl.render(seq=[None, None, 1, 2, 2, 3, 4, 4, 4])84 assert output == 'True,False,True,True,False,True,True,False,False,'8586 def test_scope(self, env):87 tmpl = env.from_string('{% for item in seq %}{% endfor %}{{ item }}')88 output = tmpl.render(seq=list(range(10)))89 assert not output
326 unmodified lines
modified:CHANGES+3 added
11 unmodified lines
12 in combination with `reject` and `select`.13- Added `previtem` and `nextitem` to loop contexts, providing access to the14 previous/next item in the loop. If such an item does not exist, the value is15 undefined.16- Added `changed(*values)` to loop contexts, providing an easy way of checking17 whether a value has changed since the last iteration (or rather since the18 last call of the method)1920Version 2.9.621-------------22(bugfix release, released on April 3rd 2017)
504 unmodified lines