Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 753
nak Avatar asked Sep 07 '14 17:09

nak


2 Answers

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.

like image 86
Penguin2600 Avatar answered Oct 18 '22 13:10

Penguin2600


You can use the syntax:

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

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

like image 29
Ray_Poly Avatar answered Oct 18 '22 13:10

Ray_Poly