From fb8432ca16577acdf33c0f2dc3e71f9f28ce2b9d Mon Sep 17 00:00:00 2001 From: EricFromCanada Date: Mon, 22 Feb 2021 17:15:17 -0500 Subject: [PATCH] Mention usage of `empty` and EmptyDrop --- _basics/types.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/_basics/types.md b/_basics/types.md index dd57044..84bdc34 100644 --- a/_basics/types.md +++ b/_basics/types.md @@ -3,13 +3,14 @@ title: Types description: An overview of data types in the Liquid template language. --- -Liquid objects can have one of five types: +Liquid objects can be one of six types: - [String](#string) - [Number](#number) - [Boolean](#boolean) - [Nil](#nil) - [Array](#array) +- [EmptyDrop](#emptydrop) You can initialize Liquid variables with the [assign]({{ "/tags/variable/#assign" | prepend: site.baseurl }}) or [capture]({{ "/tags/variable/#capture" | prepend: site.baseurl }}) tags. @@ -124,3 +125,31 @@ Adam You cannot initialize arrays using only Liquid. You can, however, use the [split]({{ "/filters/split/" | prepend: site.baseurl }}) filter to break a string into an array of substrings. + +## EmptyDrop + +An EmptyDrop object is returned if you try to access a deleted object. In the example below, `page_1`, `page_2` and `page_3` are all EmptyDrop objects: + +```liquid +{% raw %} +{% assign variable = "hello" %} +{% assign page_1 = pages[variable] %} +{% assign page_2 = pages["does-not-exist"] %} +{% assign page_3 = pages.this-handle-does-not-exist %} +{% endraw %} +``` + +### Checking for emptiness + +You can check to see if an object exists or not before you access any of its attributes. + +```liquid +{% raw %} +{% unless pages == empty %} +

{{ pages.frontpage.title }}

+
{{ pages.frontpage.content }}
+{% endunless %} +{% endraw %} +``` + +Both empty strings and empty arrays will return `true` if checked for equivalence with `empty`.