12

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?

nak
  • 3,077
  • 3
  • 27
  • 35

2 Answers2

34

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.

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Penguin2600
  • 356
  • 3
  • 2
5

You can use the syntax:

<% '' if posts.length else 'No project posts yet!' %>

https://mozilla.github.io/nunjucks/templating.html#if-expression

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Ray_Poly
  • 158
  • 1
  • 7