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

82,837

Solution 1

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 :

Solution 2

{% 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.

Solution 3

I'd rather use just trim and empty:

{% if foo|trim is empty %} 

{% if foo|trim is not empty %} 

empty evaluates to true if the foo variable is:

  • null
  • false
  • empty array
  • empty string

Solution 4

{% if foo|trim %} seems to be enough (assuming that foo is the variable to check). If foo is not null, trim removes whitespaces. Also, if handles empty string or null as false, and true otherwise, so no more is required.

References:

Share:
82,837
Guillermo Gutiérrez
Author by

Guillermo Gutiérrez

Currently Javascript Senior Developer. I have experience primarily with C# and SQL Server, which I used the most in last 2 jobs. Also I have been using Developer Express components for Windows Forms and ASP.NET MVC Framework. Aditionally I have knowledges in relational database design. Currently I am also learning Elixir, Erlang and Clojure (I love FP!) In web development (which I like the most and is what influenced me to choose this career :), I have experience with: HTML, CSS and Javascript. JQuery and some AngularJS. Bootstrap framework. Symfony PHP Framework 1.4 and 2, combined with Doctrine 1 and 2 ORM. Experience on ASP.NET MVC Framework, Web API, NHibernate, FluentNHibernate, FluentValidation and StructureMap. Basic knowledges of ServiceStack. Mobile applications with Phonegap/Cordova, using Intel's AppFramework, and now starting with Onsen UI. Scala, with Play Framework, Akka and Slick (still learning). Other programming languages I have used: C, Visual Basic 6 and .NET, DrScheme, Prolog.

Updated on July 09, 2022

Comments

  • Guillermo Gutiérrez
    Guillermo Gutiérrez almost 2 years

    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)

  • DarkWanderer
    DarkWanderer about 10 years
    I'm not twig specialist, but shouldn't it be {% if foo is not null and foo is not empty %}? Otherwise seems like it will be always true.
  • Marco Faustinelli
    Marco Faustinelli almost 9 years
    too much of a hack, imho
  • Marco Faustinelli
    Marco Faustinelli almost 9 years
    @alanTiemblo - the most voted answer, I'd say. Still imho :-)
  • Sandra
    Sandra over 2 years
    the tests in twig link is broken, twig docs now live at symfony.com - eg twig.symfony.com/doc/3.x/tests/empty.html
  • SirDerpington
    SirDerpington over 2 years
    @Sandra Thank you for the hint, I've edited all links accordingly :)
  • sschwei1
    sschwei1 about 2 years
    @SirDerpington is the foo is (not) null part still necessary? Twig documentation states that empty also checks if the value equals null
  • SirDerpington
    SirDerpington about 2 years
    @sschwei1 that's a good catch. According to the documentation that's indeed true. Not sure how it was back in 2014 though