I'd be tempted to do it as a filter...something like
def look_behind(iterable, default):
prev = default
while True:
next = iterable.next()
yield prev, next
prev = next
def look_ahead(iterable, default):
i = iter(iterable)
a = b = default
try:
a = i.next()
while True:
b = i.next()
yield a, b
a = b
except StopIteration:
yield a, default
which, after registering them as filters (adjusting as desired),
you can use them like
{% for cur, next in things|look_ahead %}
doing something with {{ cur }} and {{ next }}
{% endfor %}
or similarly looking backwards:
{% for prev, cur in things|look_ahead:"FIRST" %}
doing something with {{ prev }} and {{ cur }}
{% endfor %}
-tim
I can't help but get the feeling that there is another way to skin
your cat. However, without knowing exactly what you're trying to do,
its difficult to tell for certain.
> Is there sufficient interest in this that I should consider going the extra
> mile by opening a ticket and submitting a patch?
I can't vouch for the community as a whole, but as fair warning: the
core developers are preoccupied with delivering v1.0 at the moment, so
you may find that suggestions about improvements that aren't on the
schedule for v1.0 will probably get ignored for a little while
> Or (please excuse my
> ignorance!) is opening a ticket actually the preferred way to gauge the
> interest level?
Generally, doing both is a good idea. The ticket tracks the idea
itself - if nobody is interested, we can close the ticket, and if
anyone else has the same idea later on, we can point them at the
ticket. The mailing list enables you to establish who is interested in
the idea, and nail down the little details.
Yours,
Russ Magee %-)
I'm considering adding a forloop.previous (or somesuch) variable to the
supplemental variables produced by the for tag. If forloop.first is true,
this value would always be None, but otherwise it would reference the actual
loop item from the prior iteration.
I can't help but get the feeling that there is another way to skin
your cat. However, without knowing exactly what you're trying to do,
its difficult to tell for certain.