"if" should consider an empty array to be false
See original GitHub issuenunjucks currently considers an empty array to be true. It is common practice in jinja and its other derivatives to test for an empty array by just writing {% if arrayname %}.
From the jinja documentation:
The if statement in Jinja is comparable with the if statements of Python. In the simplest form you can use it to test if a variable is defined, not empty or not false:
{% if users %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{% endif %}
Issue Analytics
- State:
- Created 10 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Why do empty JavaScript arrays evaluate to true in conditional ...
arrays are objects, objects are truthy. just ask for array.length, if not zero, it will be truthy. when you explicitly convert to Boolean,...
Read more >In JS, is an empty Array return false or true? - holy.kiwi
Because Array is type of object , the fact that an empty Array is conversed to true is correct. · But in Loose...
Read more >How to Check if a JavaScript Array is Empty or Not with .length
To check if an array is empty or not, you can use the .length property. The length property sets or returns the number...
Read more >How to check if an array is empty using Javascript? - Flexiple
First we check if a variable is an array using the Array. · If the variable passed is an array then the condition...
Read more >Why is [] (empty Array or list) falsey in Python but, truthy in ...
[1] Here are most of the built-in objects considered false: constants defined to be false: None and False. zero of any numeric type:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, this one had me scratching my head. What is the workaround? update: this worked for me
{% if users|length %}
In my opinion, this falls into one of the side-effects of Javascript vs Python.
In python:
And Javascript:
So I think
{% if arr.length %}
is more Javascript-y, and the current logic is valid. Just my opinion.