What construct should I use to check whether a value is NULL in a Twig template?
8 Answers
Depending on what exactly you need:
is null
checks whether the value isnull
:{% 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 %}
-
76And to check if something is not null, simply do `{% if var is not null %}` – J.-B. C. Sep 06 '12 at 19:31
-
5quick note that: $var == null will return true with $var is 0, but $var is null will return false – daSn0wie Feb 13 '13 at 21:32
-
Also note that $var == 0 will return true if $var is null – Petter Soderlund Jun 11 '14 at 13:13
-
1In addition, unlike the PHP `isset()` function, `is defined` will return `true` if a variable is defined and it's value is null. – Pavel Petrov Sep 19 '17 at 07:59
-
3Notice: as Twig 2.x check for variable is equal to value like `is_ sameas` must be `{% if var is same as(false) %}` not `{% if var is sameas(false) %}` see Doc url => https://twig.symfony.com/doc/2.x/tests/sameas.html – ahmed hamdy Jul 15 '19 at 17:01
-
To check if it's empty {% if var is empty %}{% endif %} – Daniel Guerrero Aug 07 '19 at 10:32
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("") }}

- 9,408
- 18
- 74
- 116

- 4,933
- 2
- 18
- 8
-
1
-
6Seems like this is the correct way to check ... Shame it doesn't have many upvotes. – mr-sk Feb 29 '12 at 19:44
-
1Exactly what I was looking for. Had to make sure that you have | default which you can then try defaulting to another variable like so: {{ my_var | default(my_var2) }} I just tested, but you can even continue to chain like so: {{ my_var | default(my_var2) | default("my_var and my_var2 do not exist") }} – CTS_AE Oct 01 '13 at 10:17
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 %}

- 2,281
- 2
- 28
- 28
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.

- 43,213
- 17
- 66
- 89
-
2Kendall is right to suggest that they implement it - I've had nothing but good experiences asking for things to be implemented on Twig's github issue tracker. They're very friendly and professional. – Shabbyrobe Jul 17 '10 at 05:36
-
@kendall-hopkins Got curious, when is it appropriate to use `{if var is none}` and what is the PHP equivalent? – noisebleed Feb 07 '12 at 16:19
-
@noisebleed `{% if abcxyz is none %}` becomes `if (isset($context["abcxyz"])) { $_abcxyz_ = $context["abcxyz"]; } else { $_abcxyz_ = null; }` `if ((null === $_abcxyz_)) { echo "hi"; }`. So basically if the value is undefined or null, it will be true. – Kendall Hopkins Feb 08 '12 at 04:24
-
1@noisebleed Also `none` is an alias for `null` [ref](http://twig.sensiolabs.org/doc/tests/null.html). – Kendall Hopkins Feb 08 '12 at 04:24
-
The equivalent to this answer would also be to use `{% if var is empty %}` http://twig.sensiolabs.org/doc/tests/empty.html which translates to PHP `if (empty($var))` that evaluates against a falsey value (`!isset, null, 0, array(), "", false, "0", 0.0` ) http://php.net/manual/en/function.empty.php You can also use `{% if var is same as(var) %}` for identity (`===`). http://twig.sensiolabs.org/doc/tests/sameas.html – Will B. Feb 19 '15 at 16:09
You can also use one line to do that:
{{ yourVariable is not defined ? "Not Assigned" : "Assigned" }}

- 8,287
- 7
- 55
- 80

- 349
- 4
- 6
//test if varibale exist
{% if var is defined %}
//todo
{% endif %}
//test if variable is not null
{% if var is not null %}
//todo
{% endif %}

- 370
- 4
- 10
-
Why do you think this answer is better than the currently accepted one? – STT LCU Aug 28 '13 at 08:07
-
Welcome to StackOverflow! What is the difference from already accepted answer? Is it outdated? Why your answer is better than already existing ones? – Artemix Aug 28 '13 at 08:12
-
you can use the following code to check whether
{% if var is defined %}
var is variable is SET
{% endif %}

- 14,671
- 11
- 54
- 75

- 329
- 3
- 8
- 22
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

- 150
- 3
- 13