Learn a short and sweet way to verify if a variable is defined and not empty with a twig filter.

Content verification of variables in Twig can be pretty hard without the dump extension. Even with it, sometimes you will be lazy and you will assume the content of some variables sent from PHP to Twig, as usual a variable can be empty and according to it, you may want to do something different like printing other text etc. Any developer is used to the is empty test:

{# Or {% set variable = null %} as well #}
{% set variable = "" %}

{% if variable is empty %}
    The variable is empty.
{% else %}
    The variable is not empty.
{% endif %}

If the value of the variable is an empty string or null, the text printed will be "The variable is empty". As well you may want to check if a property inside an array is empty:

{# Or {% set variable = null %} as well #}
{% set variable = {
    "name": "Carlos",
    "lastName": "Delgado"
} %}

{% if variable.name is empty %}
    The variable is empty.
{% else %}
    The variable is not empty.
{% endif %}

In this case, variable exists and the name key is defined, so it will print "The variable is not empty". However what if the variable from where you want to verify if the key exists doesn't exist? You may need to extend your if with:

{% if variable is defined %}
    {% if variable.name is empty %}
        The variable is empty.
    {% else %}
        The variable is not empty.
    {% endif %}
{% else %}
    The variable is not defined.
{% endif %}

In this case as our variable doesn't exist, it will print "The variable is not defined or is not empty". A little bit messy and long isn't? Even ignoring that you don't want to show the message to your user that the variable is not defined but only "The variable is empty". What if I told you that there's an easier way to do it? Using the default filter !

Using the default filter

Using the default filter you can easily verify if a variable exists and is not empty simultaneously:

{% if variable.name|default %}
    The variable is not empty
{% else %}
    The variable is empty.
{% endif %}

In this case the filter verifies first if the variable "variable" exists, if it does, continues with the key in this case name, if it exists and is not empty, it returns true so the first message would be printed. By the otherside as mentioned, if the key exists but its value is empty, the condition will return false:

{# Or {% set variable = null %} as well #}
{% set variable = {
    "name": null,
    "lastName": "Delgado"
} %}

{% if variable.name|default %}
    The variable is not empty
{% else %}
    The variable is empty.
{% endif %}

Which in this case will print "The variable is empty". To learn more about the default filter, don't forget to read the official documentation of the filter in the Twig webpage.

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors