Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DotLiquid - checking for string "null or empty"

I'm using DotLiquid for some e-mail templates in my ASP.NET 4.0 Webforms app, and I'm trying to exclude a certain section of one of my e-mail templates if a given string in the data object I bind to the template is null or empty.

Checking for NULL works quite nicely:

{% if MyString != null %}

Some fancy label: {{ MyString }}
{% endif %}";

However, whatever I've tried to also include the empty string in this test has failed so far:

{% if MyString != null or MyString == empty %}

{% if MyString != null or MyString == '' %}

How can I check for "if this string is null or empty" ??

like image 700
marc_s Avatar asked Feb 27 '15 18:02

marc_s


People also ask

Is string null or empty?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

How do you check if a string is null?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.

Does empty string evaluate to null?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.

Is null a liquid?

Liquid objects can return one of seven basic types: String, Number, Boolean, Array, Dictionary, DateTime, or Null.


1 Answers

After discussion in comments, it was a simple logic mistake.

{% if MyString != null and MyString != "" %}

like image 173
Der Kommissar Avatar answered Sep 19 '22 11:09

Der Kommissar