Skip to content
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

Parent loop index for nested loop is wrong #46

Closed
tpetry opened this issue Oct 30, 2012 · 7 comments
Closed

Parent loop index for nested loop is wrong #46

tpetry opened this issue Oct 30, 2012 · 7 comments

Comments

@tpetry
Copy link

tpetry commented Oct 30, 2012

Hi, me again. Sorry for finding all those bugs ;)

Template:

{% set values = [['a', 'a', 'a'], ['b', 'b', 'b']] %}

{% for val_1 in values %}
  {% for val_2 in val_1 %}
    {{val_2}}: loop={{loop.index0}}, parent_loop={{loop.parent.loop.index0}}
  {% endfor %}
{% endfor %}

Expected Output:

a: loop=0, parent_loop=0
a: loop=1, parent_loop=0
a: loop=2, parent_loop=0
b: loop=0, parent_loop=1
b: loop=1, parent_loop=1
b: loop=2, parent_loop=1

Actual Output:

a: loop=0, parent_loop=0
a: loop=1, parent_loop=1
a: loop=2, parent_loop=2
b: loop=0, parent_loop=0
b: loop=1, parent_loop=1
b: loop=2, parent_loop=2 
@tpetry
Copy link
Author

tpetry commented Oct 31, 2012

Seems there are more bugs with nested loop. The loop.* values of the outer loop will be overwritten by the inner loop. Before running the inner loop the values are correct, after the outerloop they are wrong.

Seems the variable scope of the loop.*-variables is not set correctly.

@justjohn
Copy link
Collaborator

Yeah, looks like the for loop isn't properly setting the loop object on the context. Should be a pretty straightforward fix.

I think I'll finally have a chance to sit down today and go through all the bugs you've submitted, so look for comments and fixes later on today.

@tpetry
Copy link
Author

tpetry commented Oct 31, 2012

Hi, you don't have to invest time especially for me, do it when you've time for ;) "Fixed" all this bugs by twig's "set" command and storing local copies before calling any nested for-loop.

@justjohn
Copy link
Collaborator

justjohn commented Nov 1, 2012

This should be fixed now, nested for loops won't override the parent loop variable anymore.
I'll probably wait to tag a release until I finish up #45

@tpetry tpetry closed this as completed Nov 2, 2012
@tpetry
Copy link
Author

tpetry commented Nov 2, 2012

Parent loops are working but variables set by the "set"-command have now a wrong variable scope:

{% set final = false %}

{% set search = 'a' %}
{% set values = ['a', 'b', 'c', 'd'] %}
{% for val in values %}
    {% if val == search %}
        {% set final = true %}
    {% endif %}
{% endfor %}

{{final}}

Expected: true
Output: false

@tpetry tpetry reopened this Nov 2, 2012
@justjohn
Copy link
Collaborator

justjohn commented Nov 2, 2012

This is because loops now have their own internal context to keep the loop variable separate, which is how they should have been implemented before (it's why parent was having problems). I'll have to update the set function to affect the global context as well as the local.

justjohn added a commit that referenced this issue Nov 4, 2012
Twig.Template functions are now on the prototype.
@justjohn
Copy link
Collaborator

This is fixed and available in 0.4.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants