Weird behavior in TAL/Chameleon with nested repeats and macros


I'm trying to iterate over a nested structure. I do this by iterating over the top level items and subsequently over
all the deeper items using a recursive macro (called renderitem).

The problem is, that the repeat variable "child" key gets overriden within the macro, which brings some bad side
effects in the outer scope. For instance here, when constructing the id of the <li/> tags using repeat/child/number, the result of the inner scope's repeat/child/number will be used.

Shouldn't the old state of repeat be restored after leaving the tal:repeat block?

I prepared a minimal example for better understanding:

best regards,

i'd say no. TAL macros don't have the same scope like their calling templates. the same would happen in pure python, if you use the same variable name from a outer loop in a inner loop. just different names in the inner and outer loops.

I think this is counter-intuitive, since the documentation states, that "You use repeat variables to access information about the current repetition."

I made some further observations based on the following simple setup:

  1. print numberand repeat/child/number before entering macro

  2. print numberand repeat/child/number after existing macro

  3. compare these values.

The result is when looking after the very first usage of the macro, that outer scope local variable number keeps the same (1), whereas repeat/child/number changes from 1 to 3. Under my impression they both should be equal (1).

Workaround would of course always renaming the loop variable, so that a different key is used in repeat. But I see no evident way doing so. Especially when the macro starts calling itself.

Follow up: This issue has been already reported as bug, see here: