Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Odd behavior when assigning multiple variables within a for loop #18

Open
modmatrix opened this issue Dec 27, 2015 · 0 comments
Open

Odd behavior when assigning multiple variables within a for loop #18

modmatrix opened this issue Dec 27, 2015 · 0 comments

Comments

@modmatrix
Copy link

This is possibly a bug in liquid-node, but I'm observing it in grunt-liquid, so I'm reporting it here...

The issue is described in this Stack Overflow question. I'll reproduce the details below.

I'm seeing some strange behavior with variable assignments within a simple for loop. Here is version 1 of my code, which works as expected:

{% assign isThree = 'initial value' %}

{% for i in (1..9) %}

    {% if i == 3 %}
        {% assign isThree = true %}
    {% else %}
        {% assign isThree = false %}
    {% endif %}

    <p>{{ i }}: {{ isThree }}</p>

{% endfor %}

Basically, it loops through the numbers 1 through 9, and displays each one, along with a boolean indicating whether or not the number is three. This produces the expected output:

1: false
2: false
3: true
4: false
5: false
6: false
7: false
8: false
9: false

And here's version 2 of my code, which exhibits the strange behavior in question. It's identical to version 1, except it now assigns a variable named someOtherVar before assigning isThree:

{% assign isThree = 'initial value' %}

{% for i in (1..9) %}

    {% assign someOtherVar = 'foo' %}

    {% if i == 3 %}
        {% assign isThree = true %}
    {% else %}
        {% assign isThree = false %}
    {% endif %}

    <p>{{ i }}: {{ isThree }}</p>

{% endfor %}

This change shouldn't affect the output at all, but it does! In effect, it makes it so that values assigned to isThree aren't available until the next iteration of the loop:

1: initial value
2: false
3: false
4: true
5: false
6: false
7: false
8: false
9: false

If I move the someOtherVar assignment below the {% if %} block, then everything works fine again.

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

No branches or pull requests

1 participant