What construct should I use to check whether a value is NULL in a Twig template?
What construct should I use to check whether a value is NULL in a Twig template?
Depending on what exactly you need:
is null checks whether the value is null:
{% if var is null %}
{# do something #}
{% endif %}
is defined checks whether the variable is defined:
{% if var is not defined %}
{# do something #}
{% endif %}
Additionally the is sameas test, which does a type strict comparison of two values, might be of interest for checking values other than null (like false):
{% if var is sameas(false) %}
{# do something %}
{% endif %}
Also if your variable is an ARRAY, there are few options too:
{% if arrayVariable[0] is defined %}
#if variable is not null#
{% endif %}
OR
{% if arrayVariable|length > 0 %}
#if variable is not null#
{% endif %}
This will only works if your array is defined AND is NULL