Files
liquid/_basics/truthy-and-falsy.md
2021-04-22 17:19:13 -04:00

2.0 KiB

title, description
title description
Truthy and falsy An overview of boolean logic in the Liquid template language.

In programming, anything that returns true in a conditional is called truthy. Anything that returns false in a conditional is called falsy. All object types can be described as either truthy or falsy.

Truthy

All values in Liquid are truthy except nil and false.

In the example below, the text "Tobi" is not a boolean, but it is truthy in a conditional:

{% raw %}
{% assign name = "Tobi" %}

{% if name %}
  This text will always appear since "name" is defined.
{% endif %}
{% endraw %}

[Strings]({{ "/basics/types/#string" | prepend: site.baseurl }}), even when empty, are truthy. The example below will create empty HTML tags if page.category exists but is empty:

Input

```liquid {% raw %} {% if page.category %}

{{ page.category }}

{% endif %} {% endraw %} ```

Output

```html

```

Falsy

The falsy values in Liquid are [nil]({{ "/basics/types/#nil" | prepend: site.baseurl }}) and [false]({{ "/basics/types/#boolean" | prepend: site.baseurl }}).

Summary

The table below summarizes what is truthy or falsy in Liquid.

truthy falsy
true
false
nil
string
empty string
0
integer
float
array
empty array
page
EmptyDrop