How to check for null in Twig?

PhpTwigShort Circuiting

Php Problem Overview


What construct should I use to check whether a value is NULL in a Twig template?

Php Solutions


Solution 1 - Php

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 %}

Solution 2 - Php

How to set default values in twig: http://twig.sensiolabs.org/doc/filters/default.html

{{ my_var | default("my_var doesn't exist") }}

Or if you don't want it to display when null:

{{ my_var | default("") }}

Solution 3 - Php

Without any assumptions the answer is:

{% if var is null %}

But this will be true only if var is exactly NULL, and not any other value that evaluates to false (such as zero, empty string and empty array). Besides, it will cause an error if var is not defined. A safer way would be:

{% if var is not defined or var is null %}

which can be shortened to:

{% if var|default is null %}

If you don't provide an argument to the default filter, it assumes NULL (sort of default default). So the shortest and safest way (I know) to check whether a variable is empty (null, false, empty string/array, etc):

{% if var|default is empty %}

Solution 4 - Php

I don't think you can. This is because if a variable is undefined (not set) in the twig template, it looks like NULL or none (in twig terms). I'm pretty sure this is to suppress bad access errors from occurring in the template.

Due to the lack of a "identity" in Twig (===) this is the best you can do

{% if var == null %}
    stuff in here
{% endif %}

Which translates to:

if ((isset($context['somethingnull']) ? $context['somethingnull'] : null) == null)
{
  echo "stuff in here";
}

Which if your good at your type juggling, means that things such as 0, '', FALSE, NULL, and an undefined var will also make that statement true.

My suggest is to ask for the identity to be implemented into Twig.

Solution 5 - Php

You can also use one line to do that:

{{ yourVariable is not defined ? "Not Assigned" : "Assigned" }}

Solution 6 - Php

     //test if varibale exist
     {% if var is defined %}
         //todo
     {% endif %}

     //test if variable is not null
     {% if var is not null %}
         //todo
     {% endif %}

Solution 7 - Php

you can use the following code to check whether

{% if var is defined %}

var is variable is SET

{% endif %}

Solution 8 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFluffyView Question on Stackoverflow
Solution 1 - PhpNikiCView Answer on Stackoverflow
Solution 2 - Phplax4mikeView Answer on Stackoverflow
Solution 3 - PhpJamolView Answer on Stackoverflow
Solution 4 - PhpKendall HopkinsView Answer on Stackoverflow
Solution 5 - PhpJavier ClarosView Answer on Stackoverflow
Solution 6 - PhpthepauloView Answer on Stackoverflow
Solution 7 - PhpM.M.H.MasudView Answer on Stackoverflow
Solution 8 - PhpCagrisonView Answer on Stackoverflow