How to Check for Null in Twig

How to check for null in Twig?

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

TWIG: is defined and 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.

Check if string variable is null or empty, or full of white spaces

There are already good answers, but I give my 2 cents too:

{% if foo|length %}

I was inspired by @GuillermoGutiérrez's filter trick.

But I think |length is safer as the "0"|trim expression will evaluates to false.

References :

  • length
  • codepad
  • boolval

A NULL check in Twig Template is not working which leads to an error (very strange behavior)

As far as I know, it is not possible to conditionally override a block in twig, see also How can I conditionally override a TWIG layout block?

Check if a variable is defined AND truthy at once in Twig

There are (at least) two ways of doing this without extending Twig. A third option is to extend Twig by creating e.g. a Twig function. I would probably choose the first way (using the default filter).


By using the default filter

As @The_Unknown pointed out, you can also use the default filter:

{% if someVar|default(null) %}

You can omit passing the default value to the filter, and even omit the parentheses. Then the value will default to an empty string (which is falsey). I.e. these two are equal and valid:

{% if someVar|default() %}
{% if someVar|default %}

Whichever style you choose (default to null, omit the value or omit the parens), stick to it. Be consistent.

See TwigFiddle for a demonstration that truthy values evaluate to true and falsey values evaluate to false (based on the table below).


By setting strict_variables to false

By setting the environment variable strict_variables to false, you can skip the if someVar is defined part and do just {% if someVar %}. As described in Twig's documentation:

strict_variables boolean

If set to false, Twig will silently ignore invalid variables
(variables and or attributes/methods that do not exist) and replace
them with a null value. When set to true, Twig throws an exception
instead (default to false).

Set the variable to false when creating a Twig_Environment instance:

$twig = new Twig_Environment($loader, ['strict_variables' => false]);

If someVar is undefined, then {% if someVar %} is obviously false. The if tag's documentation page describes the edge case rules for defined variables:

The rules to determine if an expression is true or false are the same
as in PHP; here are the edge cases rules:


Value Boolean evaluation

empty string false
numeric zero false
whitespace-only string true
empty array false
null false
non-empty array true
object true

See TwigFiddle for a demonstration (strict_variables is set to false behind the "More options..." link in the header).


By extending Twig

(Disclaimer: I wrote this approach before @The_Unknown pointed out that the default filter can also be used.)

If the idea of setting strict_variables to false is too general, you can also extend Twig. I'd argue that it's better to set strict_variables to true to avoid accidental errors caused by e.g. typos in variable names, so this approach might be better.

I don't think that you can create a filter to do this, as an undefined variable would still throw an exception. You might be able to create a custom tag, test or extension (see Extending Twig for ways to extend Twig); I'm going to create a custom function as it's probably the simplest approach.

$twig->addFunction(new Twig_Function('istruthy', function($context, $var) {
return array_key_exists($var, $context) && $context[$var];
}, ['needs_context' => true]));

The ['needs_context' => true] part is essential here, as then you will have access to $context, which contains the variables present in the current context. (You can e.g. put var_dump($context) above the return statement to see it yourself.)

If you want istruthy to support checking multiple variables at once, you can do this:

$twig->addFunction(new Twig_Function('istruthy', function($context, ...$vars) {
foreach ($vars as $var) {
if (!array_key_exists($var, $context) || !$context[$var]) {
return false;
}
}

return true;
}, ['needs_context' => true]));

Then in Twig you can do:

{% if istruthy('foo') %} ... {% endif %}

{% if istruthy('foo') or istruthy('bar') %} ... {% endif %}

{# These two are the same: #}

{% if istruthy('foo') and istruthy('bar') and istruthy('baz') %} ... {% endif %}

{% if istruthy('foo', 'bar', 'baz') %} ... {% endif %}

{# Ternary operator can also be used: #}

{{ istruthy('foo') ? 'yep' : 'nope' }}

You might want to check in the istruthy function whether the arguments are strings or something else and then act accordingly. array_key_exists expects the first argument to be either a string or an integer.

How to check if a variable is equal to zero in twig and value defined

The way of checking for not null is:

{% if var is not null %}

But you can use the same as function:

{% if var is same as(0) %}
{# do something %}
{% endif %}

Ref: http://twig.sensiolabs.org/doc/tests/sameas.html



Related Topics



Leave a reply



Submit