mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Polishing up boolean logic and types
This commit is contained in:
+27
-52
@@ -2,77 +2,53 @@
|
|||||||
title: Truthy and Falsy
|
title: Truthy and Falsy
|
||||||
---
|
---
|
||||||
|
|
||||||
In programming, we describe “truthy” and “falsy” as anything that returns true or false, respectively, when used inside an if statement.
|
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.
|
||||||
|
|
||||||
## What is truthy?
|
## Truthy
|
||||||
|
|
||||||
All values in Liquid are truthy, with the exception of `nil` and `false`.
|
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:
|
In the example below, the text "Tobi" is not a boolean, but it is truthy in a conditional:
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
{% highlight liquid %}{% raw %}
|
||||||
{% assign tobi = 'Tobi' %}
|
{% assign tobi = 'Tobi' %}
|
||||||
{% if tobi %}
|
|
||||||
This will always be true.
|
{% if tobi == true %}
|
||||||
|
This condition will always be true.
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
[Strings](/themes/liquid-documentation/basics/types/#strings), even when empty, are truthy. The example below will result in empty HTML tags if `settings.fp_heading` is empty:
|
[Strings](/basics/types/#string), even when empty, are truthy. The example below will result in empty HTML tags if `settings.fp_heading` is empty:
|
||||||
|
|
||||||
<p class="input">Input</p>
|
<p class="input">Input</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight liquid %}{% raw %}
|
||||||
{% if settings.fp_heading %}
|
{% if settings.fp_heading %}
|
||||||
<h1>{{ settings.fp_heading }}</h1>
|
<h1>{{ settings.fp_heading }}</h1>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
|
|
||||||
<p class="output">Output</p>
|
<p class="output">Output</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight html %}{% raw %}
|
||||||
<h1></h1>
|
<h1></h1>
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
To avoid this, you can check to see if the string is <code>blank</code>, as follows:
|
[EmptyDrops](/basics/types/#emptydrop) are also truthy. In the example below, if `settings.page` is an empty string or set to a hidden or deleted object, you will end up with an EmptyDrop. The result is an empty `<div>`:
|
||||||
|
|
||||||
<div>
|
|
||||||
{% highlight html %}{% raw %}
|
|
||||||
{% unless settings.fp_heading == blank %}
|
|
||||||
<h1>{{ settings.fp_heading }}</h1>
|
|
||||||
{% endunless %}
|
|
||||||
{% endraw %}{% endhighlight %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr/>
|
|
||||||
|
|
||||||
An [EmptyDrop](/themes/liquid-documentation/basics/types/#empty-drop) is also truthy. In the example below, if `settings.page` is an empty string or set to a hidden or deleted object, you will end up with an EmptyDrop. The result is an undesirable empty <div>:
|
|
||||||
|
|
||||||
<p class="input">Input</p>
|
<p class="input">Input</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight html %}{% raw %}
|
||||||
{% if pages[settings.page] %}
|
{% if pages[settings.page] %}
|
||||||
<div>{{ pages[settings.page].content }}</div>
|
<div>{{ pages[settings.page].content }}</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
|
|
||||||
<p class="output">Output</p>
|
<p class="output">Output</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight html %}{% raw %}
|
||||||
<div></div>
|
<div></div>
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
|
## Falsy
|
||||||
|
|
||||||
## What is falsy?
|
The falsy values in Liquid are [nil](/basics/types/#nil) and [false](/basics/types/#boolean).
|
||||||
|
|
||||||
The only values that are falsy in Liquid are `nil` and `false`.
|
|
||||||
|
|
||||||
[nil](/themes/liquid-documentation/basics/types/#nil) is returned when a Liquid object doesn't have anything to return. For example, if a collection doesn't have a collection image, collection.image will be set to <tt>nil</tt>. Since that is “falsy”, you can do this:
|
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
|
||||||
{% if collection.image %}
|
|
||||||
<!-- output collection image -->
|
|
||||||
{% endif %}
|
|
||||||
{% endraw %}{% endhighlight %}
|
|
||||||
|
|
||||||
The value <tt>false</tt> is returned through many Liquid object properties such as <tt>product.available</tt>.
|
|
||||||
|
|
||||||
## Summary
|
## Summary
|
||||||
|
|
||||||
@@ -80,16 +56,15 @@ The table below summarizes what is truthy or falsy in Liquid.
|
|||||||
|
|
||||||
| | truthy | falsy |
|
| | truthy | falsy |
|
||||||
| ------------- |:-------------:|:-------------:|
|
| ------------- |:-------------:|:-------------:|
|
||||||
| true | × | |
|
| true | • | |
|
||||||
| false | | × |
|
| false | | • |
|
||||||
| nil | | × |
|
| nil | | • |
|
||||||
| string | × | |
|
| string | • | |
|
||||||
| empty string | × | |
|
| empty string | • | |
|
||||||
| 0 | × | |
|
| 0 | • | |
|
||||||
| 1 or 2 or 3.14 | × | |
|
| integer | • | |
|
||||||
| array | × | |
|
| float | • | |
|
||||||
| empty array | × | |
|
| array | • | |
|
||||||
| collection | × | |
|
| empty array | • | |
|
||||||
| collection with no products | × | |
|
| page | • | |
|
||||||
| page | × | |
|
| EmptyDrop | • | |
|
||||||
| EmptyDrop | × | |
|
|
||||||
|
|||||||
+30
-49
@@ -2,14 +2,14 @@
|
|||||||
title: Types
|
title: Types
|
||||||
---
|
---
|
||||||
|
|
||||||
Liquid objects can return one of six types:
|
Liquid objects can have one of six types:
|
||||||
|
|
||||||
- [string](#string)
|
- [string](#string)
|
||||||
- [number](#number)
|
- [number](#number)
|
||||||
- boolean
|
- [boolean](#boolean)
|
||||||
- nil
|
- [nil](#nil)
|
||||||
- array
|
- [array](#array)
|
||||||
- EmptyDrop
|
- [EmptyDrop](#emptydrop)
|
||||||
|
|
||||||
Liquid variables can be initialized by using the [assign](/tags/#assign) or [capture](/tags/#capture) tags.
|
Liquid variables can be initialized by using the [assign](/tags/#assign) or [capture](/tags/#capture) tags.
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ Numbers include floats and integers.
|
|||||||
{% endraw %}
|
{% endraw %}
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
### Booleans
|
### Boolean
|
||||||
|
|
||||||
Booleans are either `true` or `false`. No quotations are necessary when declaring a boolean.
|
Booleans are either `true` or `false`. No quotations are necessary when declaring a boolean.
|
||||||
|
|
||||||
@@ -65,26 +65,26 @@ Tags or outputs that return `nil` will not print anything to the page.
|
|||||||
|
|
||||||
<p class="input">Input</p>
|
<p class="input">Input</p>
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
{% highlight liquid %}{% raw %}
|
||||||
The current user is {{ user.name }}
|
The current user is {{ user.name }}
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
<p class="output">Output</p>
|
<p class="output">Output</p>
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
{% highlight text %}{% raw %}
|
||||||
The current user is
|
The current user is
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
### Arrays
|
### Array
|
||||||
|
|
||||||
Arrays hold lists of variables of any type.
|
Arrays hold lists of variables of any type.
|
||||||
|
|
||||||
#### Accessing items in arrays
|
#### Accessing items in arrays
|
||||||
|
|
||||||
To access items in an array, you can loop through each item in the array using a [for](/tags/#for) or [tablerow](/tags/#tablerow) tag.
|
To access all of the items in an array, you can loop through each item in the array using a [for](/tags/#for) or [tablerow](/tags/#tablerow) tag.
|
||||||
|
|
||||||
<p class="input">Input</p>
|
<p class="input">Input</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight liquid %}{% raw %}
|
||||||
<!-- if site.users = "Tobi", "Lina", "Tetsuro", "Adam" -->
|
<!-- if site.users = "Tobi", "Lina", "Tetsuro", "Adam" -->
|
||||||
{% for user in site.users %}
|
{% for user in site.users %}
|
||||||
{{ user }}
|
{{ user }}
|
||||||
@@ -92,17 +92,17 @@ To access items in an array, you can loop through each item in the array using a
|
|||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
<p class="output">Output</p>
|
<p class="output">Output</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight text %}{% raw %}
|
||||||
Tobi Lina Tetsuro Adam
|
Tobi Lina Tetsuro Adam
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
|
|
||||||
#### Accessing a specific item in an array
|
#### Accessing specific items in arrays
|
||||||
|
|
||||||
You can use square bracket `[ ]` notation to access a specific item in an array. Array indexing starts at zero.
|
You can use square bracket `[ ]` notation to access a specific item in an array. Array indexing starts at zero.
|
||||||
|
|
||||||
<p class="input">Input</p>
|
<p class="input">Input</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight liquid %}{% raw %}
|
||||||
<!-- if site.users = "Tobi", "Lina", "Tetsuro", "Adam" -->
|
<!-- if site.users = "Tobi", "Lina", "Tetsuro", "Adam" -->
|
||||||
{{ site.users[0] }}
|
{{ site.users[0] }}
|
||||||
{{ site.users[1] }}
|
{{ site.users[1] }}
|
||||||
@@ -110,67 +110,48 @@ You can use square bracket `[ ]` notation to access a specific item in an array.
|
|||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
<p class="output">Output</p>
|
<p class="output">Output</p>
|
||||||
{% highlight html %}{% raw %}
|
{% highlight text %}{% raw %}
|
||||||
Tobi
|
Tobi
|
||||||
Lina
|
Lina
|
||||||
Adam
|
Adam
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
#### Initializing an array
|
#### Initializing arrays
|
||||||
|
|
||||||
It is not possible to initialize an array using only Liquid.
|
You cannot initialize arrays using pure Liquid.
|
||||||
|
|
||||||
You can, howver, use the [split](/filters/#split) filter to break a single string into an array of substrings.
|
You can, however, use the [split](/filters/#split) filter to break a single string into an array of substrings.
|
||||||
|
|
||||||
## EmptyDrop
|
## EmptyDrop
|
||||||
|
|
||||||
An EmptyDrop object is returned whenever you try to access a non-existent object (for example, a collection, page or blog that was deleted or hidden) by [handle](/basics/#Handles). In the example below, `page_1`, `page_2` and `page_3` are all EmptyDrop objects.
|
An EmptyDrop object is returned if you try to access a deleted object (such as a page or post) by its [handle](/basics/#Handles). In the example below, `page_1`, `page_2` and `page_3` are all EmptyDrop objects.
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
{% highlight liquid %}{% raw %}
|
||||||
{% assign variable = "hello" %}
|
{% assign variable = "hello" %}
|
||||||
{% assign page_1 = pages[variable] %}
|
{% assign page_1 = pages[variable] %}
|
||||||
{% assign page_2 = pages["i-do-not-exist-in-your-store"] %}
|
{% assign page_2 = pages["does-not-exist"] %}
|
||||||
{% assign page_3 = pages.this-handle-does-not-belong-to-any-page %}
|
{% assign page_3 = pages.this-handle-does-not-exist %}
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
EmptyDrop objects only have one attribute, <code>empty?</code>, which is always true.
|
EmptyDrop objects only have one attribute, `empty?`, which is always *true*.
|
||||||
|
|
||||||
Collections and pages that _do_ exist do not have an <code>empty?</code> attribute. Their <code>empty?</code> is “falsy”, which means that calling it inside an if statement will return <tt>false</tt>. When using an unless statement on existing collections and pages, <code>empty?</code> will return <tt>true</tt>.
|
Collections and pages that *do* exist do not have an `empty?` attribute. Their `empty?` is “falsy”, which means that calling it inside an if statement will return *false*. When using an unless statement on existing collections and pages, `empty?` will return `true`.
|
||||||
|
|
||||||
#### Applications in themes
|
#### Checking for emptiness
|
||||||
|
|
||||||
Using the <code>empty?</code> attribute, you can check to see if a page exists or not _before_ accessing any of its other attributes.
|
Using the `empty?` attribute, you can check to see if an object exists or not before you access any of its attributes.
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
{% highlight liquid %}{% raw %}
|
||||||
{% unless pages.frontpage.empty? %}
|
{% unless pages.about.empty? %}
|
||||||
<!-- We have a page with handle 'frontpage' and it's not hidden.-->
|
<!-- This content will only print if the page with handle 'about' is not empty -->
|
||||||
<h1>{{ pages.frontpage.title }}</h1>
|
<h1>{{ pages.frontpage.title }}</h1>
|
||||||
<div>{{ pages.frontpage.content }}</div>
|
<div>{{ pages.frontpage.content }}</div>
|
||||||
{% endunless %}
|
{% endunless %}
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
It is important to see if a page exists or not first to avoid outputting empty HTML elements to the page, as follows:
|
If you don't check for emptiness first, Liquid may print empty HTML elements to the page:
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
{% highlight html %}{% raw %}
|
||||||
<h1></h1>
|
<h1></h1>
|
||||||
<div></div>
|
<div></div>
|
||||||
{% endraw %}{% endhighlight %}
|
{% endraw %}{% endhighlight %}
|
||||||
|
|
||||||
You can perform the same verification with collections as well:
|
|
||||||
|
|
||||||
{% highlight html %}{% raw %}
|
|
||||||
{% unless collections.frontpage.empty? %}
|
|
||||||
{% for product in collections.frontpage.products %}
|
|
||||||
{% include 'product-grid-item' %}
|
|
||||||
{% else %}
|
|
||||||
<p>We do have a 'frontpage' collection but it's empty.</p>
|
|
||||||
{% endfor %}
|
|
||||||
{% endunless %}
|
|
||||||
{% endraw %}{% endhighlight %}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user