Search code examples
stringtwigisnullorempty

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


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)


Solution

  • 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 :