TWIG: is defined and is not null
You need to declare the var in each check so {% if var is defined and var is not null %}
.
Wrong
{% if var is not null and var is defined %}
This does not work because a variable which is null in twig is defnied, but if you check for null first and it is not defined it will throw an error.
Correct
{% if var is defined and var is not null %}
This will work because we check if it is defined first and will abord when not. Only if the variable is defined we check if it is null.
I have always used the ?? operator to provide a 'default' value when I know a variable might not be defined. So {% if (var is defined and var is not null) %}
is equivalent to:
{% if (var ?? null) is not null %}
If you just want to check whether a value that might not be defined is truthy you can do this:
{% if (var ?? null) %}