mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.59k stars 640 forks source link

Async render call with client-side nunjucks #407

Closed cfjedimaster closed 9 years ago

cfjedimaster commented 9 years ago

I may be reading the docs wrong, but I thought the docs implied that if I used a function as my my third argument to render, I'd get an async call:

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

But I'm still seeing a warning in my browser dev tools that a sync http call is being made.

cfjedimaster commented 9 years ago

From what this user says here, http://stackoverflow.com/questions/29612704/how-do-you-load-a-nunjucks-template-async-client-side, it appears as if this may be a doc issue. It certainly works in the browser, but is still sync, even though the docs imply it will be async.

jlongster commented 9 years ago

The sync call only happens when you are using the WebLoader, which is only for development. You need to precompile your templates so that template.html is loaded from the precompiled bundle.

You only need to do asynchronous rendering when you want to write async templates, otherwise you can just use the sync API: var res = nunjucks.render(...). Still async template rendering is different from async template loading.

jlongster commented 9 years ago

Note that as of https://github.com/mozilla/nunjucks/commit/9e160e8d13bda3830e5799f75ed02ccb53550fdd the callback will always be called back asynchronously. Still, what you want is async loading.

We actually probably could do true async loading now, since we've supported async templates the generated code is structured in a way that including another template is always in a callback-form now. Initially it was required to be sync because the generated code needed it to be that way.

I've never loved doing a sync XHR, but it was another thing to push people to precompile templates which you really should always do in prod.

jlongster commented 9 years ago

I don't think there's any action here, so closing. Please file another issue if you feel strongly that we should work towards loading templates async by default.

jlongster commented 9 years ago

I got interested in this, actually. You can now load template async by configuring the WebLoader to be async:

nunjucks.configure({
  web: { async: true }
});

It's not async by default because if you aren't doing any actual async work in the templates (most people don't) there's no need to incur the cost of an async API. Most people can just do var res = nunjucks.render(...) because their templates are sync, and you always precompile them in production.

cfjedimaster commented 9 years ago

Hey - sorry I never responded. Thank you for adding this. :) Unfortunately, my video series is already 'baked' and I mentioned how you couldn't do this, but I'll see about getting an 'errata' page for it set up at OReilly.

jlongster commented 9 years ago

Cool! What video series is that?

cfjedimaster commented 9 years ago

It will be released (sold) by ORA in about a week or so. I cover/review 4 different engines.

On Fri, May 8, 2015 at 9:41 AM, James Long notifications@github.com wrote:

Cool! What video series is that?

— Reply to this email directly or view it on GitHub https://github.com/mozilla/nunjucks/issues/407#issuecomment-100254179.

Raymond Camden, Developer Advocate for MobileFirst at IBM

Email : raymondcamden@gmail.com Blog : www.raymondcamden.com Twitter: raymondcamden

Ramel commented 6 years ago

Hi Raymond, Have you done an "errata" page/video about nunjunks?

cfjedimaster commented 6 years ago

Nah - I haven't worked on the video product since the release. I'd like to - but ORA isn't doing video like that anymore I believe.