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

Can't use loop var "class" with asyncEach #743

Open
softshape opened this issue May 4, 2016 · 5 comments
Open

Can't use loop var "class" with asyncEach #743

softshape opened this issue May 4, 2016 · 5 comments

Comments

@softshape
Copy link

We've used {% include %} not only for templates, but also to insert SVG to HTML -

{% include "img/face" + user.face + ".svg" %}

It worked fine until we switched nunjucks to async mode. Here's our simple loader:

var NunjucksLoader = nunjucks.Loader.extend({
  async: true,
  getSource: function (name, callback)
  {
    $.ajax(name, {dataType: "text"})
      .done(function (data)
      {
    console.log('NunjucksLoader loaded ', name)
        var res = {src: {type: "string", obj: data}, path: name};
        callback(null, res);
      })
      .fail(function (e) {callback(e, null);});
  }
});

It works, generally, we can see 'NunjucksLoader loaded ' messages in the browser console. But SVG content won't appear on the resulting HTML. Included templates are fine, but included SVGs aren't.

Is there any fix for that?

@devoidfury
Copy link
Contributor

Is the include inside a loop? Make sure you use the asyncEach tag instead of for if you are, as the for tag compiles directly into a javascript for loop and does not work with async.

http://mozilla.github.io/nunjucks/api.html#asynchronous

@softshape
Copy link
Author

I've tested templates both with for and asyncEach, no difference beetwen them.

@carljm
Copy link
Contributor

carljm commented May 4, 2016

possibly related to #372?

@softshape
Copy link
Author

I've found the reason.

{% for class in json %} - works in sync mode
{% asyncEach class in json %} - doesn't work
{% asyncEach users in json %} - works!

So the name "class" used as loop variable works in the sync and fails in async mode. I've changed it and the template is OK now.

@carljm
Copy link
Contributor

carljm commented May 6, 2016

This still seems like a bug that ideally would be fixed, if your analysis of the problem is correct.

@carljm carljm reopened this May 6, 2016
@carljm carljm changed the title Including SVGs in async mode won't place them on page Can't use loop var "class" with asyncEach May 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants