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
Provenance
- Commit
bc076b3- Tree
762d8f6- Author
Adrian Moennich <adrian@planetcoding.net>- Committer
Adrian Moennich <adrian@planetcoding.net>- Parent
9bdb547
5 files changed+38 added−0 removed
modified:docs/templates.rst+13 added
616 unmodified lines617 +-----------------------+---------------------------------------------------+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++-----------------------+---------------------------------------------------+624 625 Within a for-loop, it's possible to cycle among a list of strings/variables626 each time through the loop by using the special `loop.cycle` helper::627 74 unmodified lines702 The value will increase even more!703 {% endif %}704 {% endfor %}705 706+If you only care whether the value changed at all, using `changed` is even707+easier::708+709+ {% for entry in entries %}710+ {% if loop.changed(entry.category) %}711+ <h2>{{ entry.category }}</h2>712+ {% endif %}713+ <p>{{ entry.message }}</p>714+ {% endfor %}715+716 .. _if:717 718 If719 ~~880 unmodified linesmodified:jinja2/runtime.py+8 added
358 unmodified lines359 self._undefined = undefined360 self._recurse = recurse361 self.index0 = -1362 self.depth0 = depth0363+ self._last_checked_value = missing364 365 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)]370 371+ 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 False377+378 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 linesmodified:tests/test_async.py+7 added
310 unmodified lines311 {%- endfor %}''')312 output = tmpl.render(seq=list(range(4)))313 assert output == 'x-0-1|0-1-2|1-2-3|2-3-x|'314 315+ 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,'321+322 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 output187 unmodified linesmodified:tests/test_core_tags.py+7 added
74 unmodified lines75 {%- endfor %}''')76 output = tmpl.render(seq=list(range(4)))77 assert output == 'x-0-1|0-1-2|1-2-3|2-3-x|'78 79+ 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,'85+86 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 output326 unmodified linesmodified:CHANGES+3 added
11 unmodified lines12 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)19 20 Version 2.9.621 -------------22 (bugfix release, released on April 3rd 2017)504 unmodified lines