Instrument iterables ala jinja ?

Issue #125 resolved
Michael Bayer
repo owner created an issue

i.e. user does this:

% for x in collection: % if lastelement: % endif % endif

we see the magic name "lastelement". We then compile to this:

% for lastelement, x in M_marklast(collection): % if lastelement: % endif % endif

easy !

tentative 0.3 feature. May come out.

Comments (10)

  1. Philip Jenvey

    I don't like this kind of magic. Some of the loop attributes Jinja provides could use enumerate or itertools.cycle instead, only the ones taking into account the end index of the loop aren't easily done

    But how useful are those in practice anyway? Useful enough to warrant the magic? In some simpler cases you can even replace a check for the end of the loop with a check for the beginning of the loop, e.g.

    % for foo in bar: ${} % if not last: , % endif

    1. ->

    % for i, foo in enumerate(bar): % if i != 0: , % end if ${}

  2. guest

    It's very useful to have access to first, last and cycle item. Productivity boost:)

    This is the second feature lacking in Mako (just after not having escape char for %) for my uses cases.

  3. guest

    Also keep in mind for nested loops.

    Something like this would come in handy:

    % for i in foobar as first_loop: % for j in foobar: % if first_loop.first: first! % endif ...

  4. guest

    It's safe to ignore the first loop_instrumentation patch. The second one has all the same changes plus a `from future import with_statement` for people still using python2.5.

  5. Log in to comment