FreakSoft
FreakSoft

Reputation: 393

Twig check if there are any values in the array

How can I check if any values are provided to the array. In PHP I'm adding to the array like this:

$myArray['index1'] = $someVal1;
$myArray['index2'] = $someVal2;

The problem is that when in Twig I use |length filter it gives results when $someVal1 or $someVal2 have no values (these are values taken from the form so they don't have to be filled). So I want to check if the values are not provided in the whole array, so:

{% if myArray|what_filter_here? == 0|empty|whatever %} This text should not appear {% endif %}

Can it be done in one single condition?

Upvotes: 18

Views: 40042

Answers (2)

Horaland
Horaland

Reputation: 897

Something like:

{% if myArray|length > 0 %}
    This text should not appear 
{% endif %}

Upvotes: 32

Sougata Bose
Sougata Bose

Reputation: 31739

Try with empty -

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

Upvotes: 24

Related Questions