Search code examples
nunjucks

How do you load a Nunjucks template async (client-side)?


From what I see in the docs, when you use the render api and pass a third argument for a callback, calls to render should be async, not sync, yet when I do this client-side:

nunjucks.render("template.html", data, function(err, res) {
    $("#resultDiv").html(res);
});

It is still doing a sync xhr request. (To be clear, it renders just fine, but it isn't an async call.)


Solution

  • Since no better answer showed up overnight and you requested it, my answer from the comments is:

    Looking at the library code, it appears this is the intended behavior. If you follow the render call's path, you will get to this comment in the code. I think the answer to your question would likely be to override this loader in some form or fashion (fork the repo, monkey-patch it, etc).