10

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" ??

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You should probably use double quotes (`MyString == ""`), the other fault is likely in your logic. You say "If MyString is NOT null, or MyString is '' (empty), then show this area" essentially. I think you mean: `{% if MyString != null and MyString != "" %}` – Der Kommissar Feb 27 '15 at 18:29
  • @mark_s Did you modify your statement as I suggested? (`{% if MyString != null and MyString != "" %}`) – Der Kommissar Feb 27 '15 at 19:24
  • @EBrown: sorry - I missed that - and **yes**, now it works! Please post as answer so I can accept it ! – marc_s Feb 27 '15 at 19:26

3 Answers3

21

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

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

Der Kommissar
  • 5,848
  • 1
  • 29
  • 43
2

There are some built in functions to handle this.

String.IsNullOrEmpty

and

String.IsNullOrWhiteSpace

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • I'm going to assume it will look like {% if String.IsNullOrEmpty(MyString) %}. But I've never actually used this library so I can't be sure. – Bradley Uffner Feb 27 '15 at 19:37
  • Thanks, Bradley - yes, indeed, that syntax just works - great to know! The DotLiquid documentation is a bit thin at times, and the more generic Liquid docs of course don't mention .NET specifics.... great to know these function are available in DotLiquid and work as expected! – marc_s Feb 27 '15 at 19:39
  • @BradleyUffner Are you sure this works? Because it's not working for me, but the accepted answer does. – Uğur Dinç Jun 18 '20 at 15:09
0

Would it be:

MyString == String.Empty

Just looking at how DotLiquid evaluates expressions:
https://github.com/dotliquid/dotliquid/blob/master/src/DotLiquid/Condition.cs

Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
  • Doesn't work for me :-( Same result - even though my data string is `string.Empty`, the section I want to exclude is still showing up in the result – marc_s Feb 27 '15 at 19:22