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

repeated set appears to be malfunctioning #119

Closed
Pomax opened this issue Aug 16, 2013 · 6 comments
Closed

repeated set appears to be malfunctioning #119

Pomax opened this issue Aug 16, 2013 · 6 comments

Comments

@Pomax
Copy link
Contributor

Pomax commented Aug 16, 2013

I'm using the following nunjucks fragment:

      {% set title = "photostream" %}
      {% set prev = photo.prev %}
      {% set next = photo.next %}
      {% include "streamnav.html" %}

      {% for setObj in photo.sets %}
        {% set photoset = photosets[setObj.id] %}
        {% set title = photoset.title._content %}
        {% set prev = setObj.prev %}
        {% set next = setObj.next %}
        {% include "streamnav.html" %}
      {% endfor %}
    </div>

However, when templating is performed inside the for loop, the title and other variables, despite having been rebound using the set command, are still the value they received on first `set.

@mattbasta
Copy link
Contributor

Can you pare down your snippet into a standalone example? I think I know why this is happening and have an idea how to fix it, but want to make sure it works with your use case.

@Pomax
Copy link
Contributor Author

Pomax commented Aug 16, 2013

no problem. Reduced:

test.html

{% set a = b %}
{% include "a.html" %}
{% for c in d %}
  {% set a = c %}
  {% include "a.html" %}
{% endfor %}

a.html

a = {{ a }}

app.js

app.get('/test', function(req, res) {
  res.render("test.html", {
    b: "b",
    d: ["1", "2", "3"]
  })
})

mattbasta added a commit to mattbasta/nunjucks that referenced this issue Aug 19, 2013
@mattbasta
Copy link
Contributor

So this is why you're seeing the issue:

  1. When you do the original set a = b, the compiler generates a context.set('item', b)[1].
  2. When you do set a = c, the compiler generates frame.set('item', c).
  3. When you do {{ a }} in a.html, the compiler generates runtime.contextOrFrameLookup(context, frame, 'item').
  4. contextOrFrameLookup does a context.lookup('item') and sees that it has a value (b's value) and returns that.

I'm not sure why contextOrFrameLookup looks at context before frame, as that seems like a bug (frame will always override the context, I think). I have a PR that fixes the issue, but I'll let @jlongster look at it and see if there's a strong reason to not flip the context/frame lookup order.

[1] Simplified for the sake of explanation

jlongster added a commit that referenced this issue Aug 21, 2013
Reverse order of context/frame lookup (issue #119)
@jlongster
Copy link
Contributor

@Pomax does this fix your issue?

@Pomax
Copy link
Contributor Author

Pomax commented Sep 9, 2013

Completely missed the pull request - I'll test this in a few hours!

@Pomax
Copy link
Contributor Author

Pomax commented Sep 10, 2013

@jlongster yup: that seems to do the trick!

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

3 participants