if... !true conditional rendering in nunjucks

nak picture nak · Sep 7, 2014 · Viewed 24.4k times · Source

if...true conditionals work like a charm as outlined here in the docs.

but if I try to do something like:

{% if !posts.length %}
<i>No project posts yet!</i>
{% endif %}

I get an error:

Template render error: (/home/nak/clones/mf3/views/project.html) [Line 10, Column 9]
 unexpected token: !

I've worked around this by doing:

{% if posts.length %}
{% else %}
<i>No project posts yet!</i>
{% endif %}

Is there a better (correct) way to do this?

Answer

Penguin2600 picture Penguin2600 · Sep 7, 2014

I see you've got a bit of a bobby dazzler here.

Try using not instead of !.

In other words, use not, not !!

Give 'er a go mate and notice that in the raw section here they highlight not as if it's a keyword.

https://mozilla.github.io/nunjucks/templating.html#raw

Best of luck to ye.