Search code examples
templatesnunjucks

if... !true conditional rendering in nunjucks


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?


Solution

  • 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.