-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repeat.item.number and repeat.item.index don't seem to work with recursion #177
Comments
But you're redefining |
Sorry it wasn't clear. For recursion purposes to display parents and children to arbitrary depth. And it's possible my expectations are off here. For example a list of things where each thing has children: class Thing():
def __init__(self):
self.children = None
def return_things():
''' create 2 parents, each with 3 children '''
thing1 = Thing()
thing2 = Thing()
thing1.children = [Thing() for x in xrange(0,3)]
thing2.children = [Thing() for x in xrange(0,3)]
return [thing1, thing2] The template displays Thing1 as Index 0. It then loads the macro "thing_list" on the list retrieved from Thing1.children in a recursive manner, by assigning 'thing' to 'item.children'. This works fine and generates the recursion to any depth based on any parent-child I send. The only "issue" is that the repeat.item.index and repeat.item.number don't behave as I'd expect. If I were writing this as a function, it would be only in the scope of this function call. Perhaps I'm mistaken expecting scope rules to apply to tags like they apply to functions calls. But I'm not sure what the decrement means. So I expected that the redefinition of the variable "thing" would only have scope in this area: <div tal:define="thing item.children">
<div metal:use-macro="template.macros['thing_list']" />
</div> When running the template over the data, I'd expect to see:
and if I had a 3rd thing:
|
It is a little weird I must admit, especially the negative values. |
The more I look at it, the more I think it's the scope. I'm guessing the repeat.item (as opposed to item itself) is being consumed or something and set to 0, then passed up to the calling scope, which checks it, and says "it's consumed". so while the item itself is scoped fine, the repeat.item is not? Here is the output from a larger example. All the item's are properly nested and accessed recursively by the macro, but the leading number on each line is the repeat.item.number. HTH. I can get the behavior I want a different way, but was suggested I bring this to your attention. please excuse the test comments, they weren't originally for public consumption |
I'll need to look at the generated source and step through to really On 15 July 2014 21:57, marcidy [email protected] wrote:
|
No worries, I'm going to use a work-around regardless. This is just a demo. Thanks for your attention. |
I'm experiencing the very same issue. I have posted an example here: https://community.plone.org/t/weird-behavior-in-tal-chameleon-with-nested-repeats-and-macros/453 One issue seems to be, that any previous state of the @marcidy I'm curious, what's your work-around for this? |
My work around for this issue – pending being fixed – is to use:
This gives you an explicit iterator index as a scoped variable. But I think it's a nasty bug that should be fixed. I took a brief look and it's not clear how to best go about it because the repeat dictionary is very simple and not "scoped". |
I never posted my work-around (or actual solution?), sorry about that. 1<div class="step_list" metal:define-macro="step_list" >
2 <div tal:repeat="step steps">
3 < all other stuff />
4 <div tal:define="steps step.children">
5 <div metal:use-macro="template.macros['step_list']" />
6 </div> Forgive the abuse of notation below. "Step0" and "Step1" are both variables with the label "step".
line 2 is a potential issue with this, the label "steps" is reassigned but the original list of steps still exists to be iterated after the macro exists. Not sure if this is a problem or a feature. Feature to me right now : ) Hope this can help in some way. Let me know if there's anything else I can do. Again, apologies for not posting this before, especially to @pgrunewald |
This appears to be a matter of local scopes not being cleaned up when the local scope is left. Consider this simplistic example with a list of lists. The
The result:
The example shows that the inner repeat loop initializes the |
When using a recursive structure such as below, the repeat.item.number and index don't appear to behave in a sane way. I'm not sure why they decrement once, then stay the same for the current level. They do seem to reset on recursion, but the behavior of next and subsequent values isn't what I would expect.
The text was updated successfully, but these errors were encountered: