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

Guillermo Gutiérrez picture Guillermo Gutiérrez · Apr 9, 2014 · Viewed 67.1k times · Source

How can I check if a string variable is null or empty, or full with space characters in Twig? (Shortest possible, maybe an equivalent to CSharp's String.IsNullOrWhiteSpace() method)

Answer

SirDerpington picture SirDerpington · Apr 9, 2014
{% if your_variable is null or your_variable is empty %}

should check whether the variable is null or empty.

If you want to see if it's not null or empty just use the notoperator.

 {% if foo is not null and foo is not empty %}

See the docs:

Perhaps you might be interested in tests in twig generally.