Skip to content
Myr

Documented loop.depth and added loop.depth0.

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

authored by Armin Ronacher · committed by Armin Ronacher · parent dcd0cb7 · Browse files · 5 files+34 added−4 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+34 added−4 removed

  • other:+34 added−4 removed

5 files+34 added−4 removed

modified:docs/templates.rst+6 added
574 unmodified lines
575+-----------------------+---------------------------------------------------+576| `loop.cycle` | A helper function to cycle between a list of |577| | sequences. See the explanation below. |578+-----------------------+---------------------------------------------------+579| `loop.depth` | Indicates how deep in deep in a recursive loop |580| | the rendering currently is. Starts at level 1 |581+-----------------------+---------------------------------------------------+582| `loop.depth0 | Indicates how deep in deep in a recursive loop |583| | the rendering currently is. Starts at level 0 |584+-----------------------+---------------------------------------------------+585586Within a for-loop, it's possible to cycle among a list of strings/variables587each time through the loop by using the special `loop.cycle` helper::588
847 unmodified lines
modified:jinja2/testsuite/core_tags.py+20 added
83 unmodified lines
84 dict(a=2, b=[dict(a=1), dict(a=2)]),85 dict(a=3, b=[dict(a='a')])86 ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]'8788 def test_recursive_depth0(self):89 tmpl = env.from_string('''{% for item in seq recursive -%}90 [{{ loop.depth0 }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]91 {%- endfor %}''')92 self.assertEqual(tmpl.render(seq=[93 dict(a=1, b=[dict(a=1), dict(a=2)]),94 dict(a=2, b=[dict(a=1), dict(a=2)]),95 dict(a=3, b=[dict(a='a')])96 ]), '[0:1<[1:1][1:2]>][0:2<[1:1][1:2]>][0:3<[1:a]>]')9798 def test_recursive_depth(self):99 tmpl = env.from_string('''{% for item in seq recursive -%}100 [{{ loop.depth }}:{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]101 {%- endfor %}''')102 self.assertEqual(tmpl.render(seq=[103 dict(a=1, b=[dict(a=1), dict(a=2)]),104 dict(a=2, b=[dict(a=1), dict(a=2)]),105 dict(a=3, b=[dict(a='a')])106 ]), '[1:1<[2:1][2:2]>][1:2<[2:1][2:2]>][1:3<[2:a]>]')107108 def test_looploop(self):109 tmpl = env.from_string('''{% for row in table %}110 {%- set rowloop = loop -%}111 {% for cell in row -%}
194 unmodified lines
modified:jinja2/compiler.py+2 added-2 removed
1064 unmodified lines
1065 aliases = self.push_scope(loop_frame, ('loop',))10661067 # otherwise we set up a buffer and add a function def1068 else:1069 self.writeline('def loop(reciter, loop_render_func):', node)1069 self.writeline('def loop(reciter, loop_render_func, depth=0):', node)1070 self.indent()1071 self.buffer(loop_frame)1072 aliases = {}1073
50 unmodified lines
1124 else:1125 self.visit(node.iter, loop_frame)11261127 if node.recursive:1128 self.write(', recurse=loop_render_func):')1128 self.write(', loop_render_func, depth):')1129 else:1130 self.write(extended_loop and '):' or ':')11311132 # tests in not extended loops become a continue
515 unmodified lines
modified:jinja2/runtime.py+4 added-2 removed
278 unmodified lines
279280class LoopContext(object):281 """A loop context for dynamic iteration."""282283 def __init__(self, iterable, recurse=None):283 def __init__(self, iterable, recurse=None, depth0=0):284 self._iterator = iter(iterable)285 self._recurse = recurse286 self._after = self._safe_next()287 self.index0 = -1288 self.depth0 = depth0289290 # try to get the length of the iterable early. This must be done291 # here because there are some broken iterators around where there292 # __len__ is the number of iterations left (i'm looking at your
13 unmodified lines
306 last = property(lambda x: x._after is _last_iteration)307 index = property(lambda x: x.index0 + 1)308 revindex = property(lambda x: x.length - x.index0)309 revindex0 = property(lambda x: x.length - x.index)310 depth = property(lambda x: x.depth0 + 1)311312 def __len__(self):313 return self.length314
10 unmodified lines
325 def loop(self, iterable):326 if self._recurse is None:327 raise TypeError('Tried to call non recursive loop. Maybe you '328 "forgot the 'recursive' modifier.")327 return self._recurse(iterable, self._recurse)329 return self._recurse(iterable, self._recurse, self.depth0 + 1)330331 # a nifty trick to enhance the error message if someone tried to call332 # the the loop without or with too many arguments.333 __call__ = loop
238 unmodified lines
modified:CHANGES+2 added
26 unmodified lines
27- Added finer grained support for stripping whitespace on the left side28 of blocks.29- Added `map`, `select`, `reject`, `selectattr` and `rejectattr`30 filters.31- Added support for `loop.depth` to figure out how deep inside a recursive32 loop the code is.3334Version 2.635-----------36(codename Convolution, released on July 24th 2011)
263 unmodified lines