HI all,
we have a template like this:
<a href="#/classes" class="button back">{% include "img/button/back.svg" %}</a>
It's loaded with custom async loader. And it works.
Now I need to wrap it with a condition:
{% if json.students.length %}
<a href="#/classes" class="button back">{% include "img/button/back.svg" %}</a>
{% endif %}
And now template rendering stops on "include" statement. If I remove the condition, it works as usual.
It looks like a bug, while I'd welcome any workaround also.
Could you please provide fully working example?
@vecmezoni This happens to me too.
Any import inside a condition stops the template rendering.
See below, anything is printed.
{% if enabledNewFooter %}
{% include "new_footer.html" %}
{% else %}
{% include "footer.html" %}
{% endif %}
@softshape You can use this for now:
{% set templateFile = 'a.html' if condition else 'b.html' %}
{% include "directory/" + templateFile %}
@andersonba does this happen only with async loader?
@vecmezoni I only tested with async loader using express-nunjucks.
Do you have any idea about this bug? I could fix it.
@vecmezoni, any idea?
I've ran into this issue twice now with AdonisJs, it does the same thing if you use an include inside a macro, I fudged adonis to not use async and it works completely fine
Just made a super bare bones example to prove the point, first glance seems to be its calling the render callback before the sub template has even read from the filesystem.
So some painful hours later I have a working solution - https://github.com/almightyju/nunjucks
It's using promises in the dynamic functions that get made which I'm not sure if you didn't use them for any reason but the a-sync-waterfall stuff you was using was indeed the problem, in the root template it would trigger the waterfall to render an include which then was ignored and an empty string value was instantly returned to the root.
So I decided to change it to make a promise chain then pass it back, which then lead me down the rabbit hole but I have a working system for what I'm using right now, it does basically mean its not really a choice of "sync" or "async" anymore so I'm not sure if that is going to have any impact...
But, it's not fully tested since I haven't used all the features and I started chasing my tail a bit fixing one thing which triggered more broken things so I expect it's going to have some hidden problems as it is currently. I've used blocks, extends, for, variable output, macro and include, i think thats all I've used anyway.
Happy to help out some more if I know what you guys want or you can just lift my commit or I can make a pull request.
Time for sleep :sleeping:
I suspect there is a tag {% ifAsync %}
https://github.com/mozilla/nunjucks/blob/master/src/nodes.js#L93
I suspect there is a tag
{% ifAsync %}
https://github.com/mozilla/nunjucks/blob/master/src/nodes.js#L93
Using ifAsync finally solve my issue... I wonder why this is not in the document..
Most helpful comment
I suspect there is a tag
{% ifAsync %}https://github.com/mozilla/nunjucks/blob/master/src/nodes.js#L93