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 nullchecks whether the value isnull:
{% if var is null %}
{# do something #}
{% endif %}
is definedchecks 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 %}
6 Comments
{% if var is not null %}isset() function, is defined will return true if a variable is defined and it's value is null.is_ sameas must be {% if var is same as(false) %} not {% if var is sameas(false) %} see Doc url => twig.symfony.com/doc/2.x/tests/sameas.htmlHow 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("") }}
2 Comments
undefined or empty and null?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 %}
Comments
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.
5 Comments
{if var is none} and what is the PHP equivalent?{% 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.{% if var is empty %} 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 ) php.net/manual/en/function.empty.php You can also use {% if var is same as(var) %} for identity (===). twig.sensiolabs.org/doc/tests/sameas.html //test if varibale exist
{% if var is defined %}
//todo
{% endif %}
//test if variable is not null
{% if var is not null %}
//todo
{% endif %}
3 Comments
if var is not null.