Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for empty variable in Swig template

In Twig I have operator is and test for empty variable (string or array):

{% if info is empty %}
    ...
{% endif %}

How I can do something like this in Swig template?

like image 707
inetbug Avatar asked Oct 14 '13 19:10

inetbug


2 Answers

Simply do

{% if !info.length %}
...
{% endif %}

This will match strings (""), arrays ([]) and any other object which doesn't have a .length property with truthy value.

like image 194
gustavohenke Avatar answered Oct 12 '22 23:10

gustavohenke


{% if Object.keys(info).length != 0 %}

for an object/dict empty test

like image 33
Baryon Lee Avatar answered Oct 12 '22 23:10

Baryon Lee