From d63213d46325f01ff016b52ff6136e7dc2eaeb0f Mon Sep 17 00:00:00 2001 From: Adam Hollett Date: Thu, 15 Oct 2015 14:52:13 -0400 Subject: [PATCH 1/3] Minor updates, switch Pigments to Rouge --- _basics/handles.md | 36 +++++--------------- _basics/operators.md | 72 +++++++++++----------------------------- _config.yml | 1 + _sass/modules/_base.scss | 4 +++ 4 files changed, 33 insertions(+), 80 deletions(-) diff --git a/_basics/handles.md b/_basics/handles.md index e348450..c91e45a 100644 --- a/_basics/handles.md +++ b/_basics/handles.md @@ -13,7 +13,7 @@ For example, a page with the title "About Us" can be accessed in Liquid via its {% endraw %} {% endhighlight %} -## Creating handles +### Creating handles An object with the title "Shirt" will automatically be given the handle `shirt`. If there is already an object with the handle `shirt`, the handle will auto-increment. In other words, "Shirt" objects created after the first one will receive the handle `shirt-1`, `shirt-2`, and so on. @@ -23,46 +23,28 @@ Handles also determine the URL of their corresponding objects. For example, a pa Websites often rely on static handles for pages, posts, or objects. To preserve design elements and avoid broken links, if you modify the title of an object, **its handle is not automatically updated**. For example, if you were to change a page title from "About Us" to "About This Website", its handle would still be `about-us`. -You can change an object's handle manually (how?) +You can change an object's handle manually (TK how to change a handle manually) -## Accessing attributes via the handle", "attributes-handle +### Accessing handle attributes In many cases you may know the handle of a object whose attributes you want to access. You can access its attributes by pluralizing the name of the object, then using either the square bracket ( [ ] ) or dot ( . ) notation.

Input

-{% highlight html %}{% raw %} +{% highlight liquid %} +{% raw %} {{ pages.about-us.title }} {{ pages["about-us"].title }} -{% endraw %}{% endhighlight %} +{% endraw %} +{% endhighlight %}

Output

-{% highlight html %}{% raw %} +{% highlight text %} About Us About Us -{% endraw %}{% endhighlight %} +{% endhighlight %}
In the example above, notice that we are using `pages` as opposed to `page`. - -You can also pass in Customize theme page objects using this notation. This is handy for theme designers who wish to give the users of their themes the ability to select which content to display in their theme. - -

Input

-
-{% highlight html %}{% raw %} -{% for product in collections[settings.home_featured_collection].products %} - {{ product.title }} -{% endfor %} -{% endraw %}{% endhighlight %} -
- -

Output

-
-{% highlight html %}{% raw %} -Awesome Shoes -Cool T-Shirt -Wicked Socks -{% endraw %}{% endhighlight %} -
diff --git a/_basics/operators.md b/_basics/operators.md index 2d63208..6719aa5 100644 --- a/_basics/operators.md +++ b/_basics/operators.md @@ -2,7 +2,7 @@ title: Operators --- -Liquid has access to all of the logical and comparison operators. +Liquid includes many logical and comparison operators. ### Basic Operators @@ -17,7 +17,7 @@ Liquid has access to all of the logical and comparison operators. does not equal -
>
+
>
greater than @@ -25,9 +25,8 @@ Liquid has access to all of the logical and comparison operators. less than -
>=
- greater than or equal to - +
>=
+ greater than or equal to
<=
@@ -35,84 +34,51 @@ Liquid has access to all of the logical and comparison operators.
or
- condition A or condition B + logical or
and
- condition A and condition B + logical and - + -**Examples:** +For example:
-{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} {% if product.title == "Awesome Shoes" %} - These shoes are awesome! + These shoes are awesome! {% endif %} {% endraw %}{% endhighlight %}
-Operators can be chained together. +You can use multiple operators in a tag:
-{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} {% if product.type == "Shirt" or product.type == "Shoes" %} - This is a shirt or a shoe. + This is a shirt or a pair of shoes. {% endif %} {% endraw %}{% endhighlight %}
+### contains +`contains` checks for the presence of a substring inside a string. - - - - - - -### Contains operator - -contains checks for the presence of a substring inside a string. - - -{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} {% if product.title contains 'Pack' %} This product's title contains the word Pack. {% endif %} {% endraw %}{% endhighlight %} +`contains` can also check for the presence of a string in an array of strings. -contains can also check for the presence of a string in an array of strings. - -{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} {% if product.tags contains 'Hello' %} This product has been tagged with 'Hello'. {% endif %} {% endraw %}{% endhighlight %} - -You **cannot** check for the presence of an object in an array of objects using contains. This will not work: - -{% highlight html %}{% raw %} -{% if product.collections contains 'Sale' %} - One of the collections this product belongs to is the Sale collection. -{% endif %} -{% endraw %}{% endhighlight %} - -This will work: - -{% highlight html %}{% raw %} -{% assign in_sale_collection = false %} -{% for collection in product.collections %} - {% if in_sale_collection == false and collection.title == 'Sale' %} - {% assign in_sale_collection = true %} - {% endif %} -{% endfor %} -{% if in_sale_collection %} - One of the collections this product belongs to is the Sale collection. -{% endif %} -{% endraw %}{% endhighlight %} - - \ No newline at end of file +`contains` is can only search strings. You cannot use it to check for an object in an array of objects. diff --git a/_config.yml b/_config.yml index 914ffa2..e87ace9 100644 --- a/_config.yml +++ b/_config.yml @@ -2,6 +2,7 @@ baseurl: "" # the subpath of your site, e.g. /blog/ url: "http://liquidmarkup.org" # the base hostname & protocol for your site markdown: kramdown +highlighter: rouge permalink: /:year/:month/:day/:basename:output_ext collections: basics: diff --git a/_sass/modules/_base.scss b/_sass/modules/_base.scss index ddd20ac..81fcdee 100644 --- a/_sass/modules/_base.scss +++ b/_sass/modules/_base.scss @@ -173,6 +173,10 @@ table { border: 1px solid #ddd; padding: 6px 13px; } + /* Don't use a margin on code samples within tables */ + pre { + margin-bottom: 0; + } } td { From f2a1925033e8c570f8d813af913bad7ea70b476d Mon Sep 17 00:00:00 2001 From: Adam Hollett Date: Thu, 15 Oct 2015 17:00:22 -0400 Subject: [PATCH 2/3] Updates to types and truthiness --- _basics/truthy-and-falsy.md | 36 ++-------- _basics/types.md | 133 +++++++++++++++--------------------- 2 files changed, 62 insertions(+), 107 deletions(-) diff --git a/_basics/truthy-and-falsy.md b/_basics/truthy-and-falsy.md index e4f3bd5..bbacdc7 100644 --- a/_basics/truthy-and-falsy.md +++ b/_basics/truthy-and-falsy.md @@ -4,9 +4,9 @@ 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. -## What is truthy? +## What is truthy? -All values in Liquid are truthy, with the exception of nil and false. +All values in Liquid are truthy, with the exception of `nil` and `false`. In the example below, the text “Tobi” is not a boolean, but it is truthy in a conditional: @@ -17,7 +17,7 @@ This will always be true. {% endif %} {% 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](/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:

Input

{% highlight html %}{% raw %} @@ -32,7 +32,7 @@ This will always be true.

{% endraw %}{% endhighlight %} -To avoid this, you can check to see if the string is blank, as follows: +To avoid this, you can check to see if the string is blank, as follows:
{% highlight html %}{% raw %} @@ -44,7 +44,7 @@ To avoid this, you can check to see if the string is blank, as foll
-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>: +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>:

Input

{% highlight html %}{% raw %} @@ -62,7 +62,7 @@ An [EmptyDrop](/themes/liquid-documentation/basics/types/#empty-drop) is also tr ## What is falsy? -The only values that are falsy in Liquid are nil and false. +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 nil. Since that is “falsy”, you can do this: @@ -76,7 +76,7 @@ The value false is returned through many Liquid object properties such ## Summary -The table below summarizes what is truthy or falsy in Liquid. +The table below summarizes what is truthy or falsy in Liquid. | | truthy | falsy | | ------------- |:-------------:|:-------------:| @@ -93,25 +93,3 @@ The table below summarizes what is truthy or falsy in Liquid. | collection with no products | × | | | page | × | | | EmptyDrop | × | | - - - - - - - - - - - - - - - - - - - - - - diff --git a/_basics/types.md b/_basics/types.md index b4e1925..da10c96 100644 --- a/_basics/types.md +++ b/_basics/types.md @@ -2,152 +2,129 @@ title: Types --- -Liquid objects can return one of six types: String, Number, Boolean, Nil, Array, or EmptyDrop. Liquid variables can be initialized by using the assign or capture tags. +Liquid objects can return one of six types: +- [string](#string) +- [number](#number) +- boolean +- nil +- array +- EmptyDrop +Liquid variables can be initialized by using the [assign](/tags/#assign) or [capture](/tags/#capture) tags. -### Strings +### String -Strings are declared by wrapping the variable's value in single or double quotes. +Strings are declared by wrapping a variable's value in single or double quotes. -
+{% highlight liquid %} {% raw %} {% assign my_string = "Hello World!" %} {% endraw %} -
+{% endhighlight %} +### Number -### Numbers +Numbers include floats and integers. -Numbers include floats and integers. - -
+{% highlight liquid %} {% raw %} -{% assign my_num = 25 %} +{% assign my_int = 25 %} +{% assign my_float = 39.756 %} {% endraw %} -
- - +{% endhighlight %} ### Booleans -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. -
+{% highlight liquid %} {% raw %} {% assign foo = true %} {% assign bar = false %} {% endraw %} -
- - +{% endhighlight %} ### Nil -Nil is an empty value that is returned when Liquid code has no results. It is **not** a string with the characters "nil". +Nil is a special empty value that is returned when Liquid code has no results. It is **not** a string with the characters "nil". -Nil is treated as false in the conditions of {% if %} blocks and other Liquid tags that check for the truthfulness of a statement. The example below shows a situation where a fulfillment does not yet have a tracking number entered. The if statement would not render the included text within it. +Nil is treated as false in the conditions of `if` blocks and other Liquid tags that check the truthfulness of a statement. +In the following example, if the user does not exist (that is, `user` returns `nil`), Liquid will not print the greeting: + +{% highlight liquid %} {% raw %} -{% if fulfillment.tracking_numbers %} -We have a tracking number! +{% if user %} + Hello {{ user.name }}! {% endif %} {% endraw %} +{% endhighlight %} -Any tags or outputs that return nil will not show anything on the screen. +Tags or outputs that return `nil` will not print anything to the page.

Input

{% highlight html %}{% raw %} -Tracking number: {{ fulfillment.tracking_numbers }} +The current user is {{ user.name }} {% endraw %}{% endhighlight %}

Output

-
{% highlight html %}{% raw %} -Tracking number: +The current user is {% endraw %}{% endhighlight %} -
- - - ### Arrays -Arrays hold a list of variables of all types. +Arrays hold lists of variables of any type. -#### Accessing all items in an array +#### Accessing items in arrays -To access items in an array, you can loop through each item in the array using a for tag or a tablerow tag. +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.

Input

-
{% highlight html %}{% raw %} - -{% for tag in product.tags %} - {{ tag }} + +{% for user in site.users %} + {{ user }} {% endfor %} {% endraw %}{% endhighlight %} -

Output

-
{% highlight html %}{% raw %} -sale summer spring wholesale +Tobi Lina Tetsuro Adam {% endraw %}{% endhighlight %} -
#### Accessing a specific item in an array -You can use square brackets ( [ ] ) 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.

Input

-
{% highlight html %}{% raw %} - -{{ product.tags[0] }} -{{ product.tags[1] }} -{{ product.tags[2] }} -{{ product.tags[3] }} + +{{ site.users[0] }} +{{ site.users[1] }} +{{ site.users[3] }} {% endraw %}{% endhighlight %} -

Output

-
{% highlight html %}{% raw %} -sale -summer -spring -wholesale +Tobi +Lina +Adam {% endraw %}{% endhighlight %} -
- #### Initializing an array -It is not possible to initialize an array in Liquid. For example, in Javascript you could do something like this: - -
-{% highlight html %}{% raw %} - -{% endraw %}{% endhighlight %} -
- -In Liquid, you must instead use the split filter to break a single string into an array of substrings. See here for examples. - - - - - +It is not possible to initialize an array using only Liquid. +You can, howver, use the [split](/filters/#split) filter to break a single string into an array of substrings. ## 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](/themes/liquid-documentation/basics/handle). In the example below, page_1, page_2 and page_3 are all EmptyDrop objects. +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. {% highlight html %}{% raw %} {% assign variable = "hello" %} @@ -156,13 +133,13 @@ An EmptyDrop object is returned whenever you try to access a non-existent object {% assign page_3 = pages.this-handle-does-not-belong-to-any-page %} {% endraw %}{% endhighlight %} -EmptyDrop objects only have one attribute, empty?, 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 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. +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 -Using the empty? 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 a page exists or not _before_ accessing any of its other attributes. {% highlight html %}{% raw %} {% unless pages.frontpage.empty? %} @@ -172,14 +149,14 @@ Using the empty? attribute, you can check to see if a page exists o {% endunless %} {% 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: +It is important to see if a page exists or not first to avoid outputting empty HTML elements to the page, as follows: {% highlight html %}{% raw %}

{% endraw %}{% endhighlight %} -You can perform the same verification with collections as well: +You can perform the same verification with collections as well: {% highlight html %}{% raw %} {% unless collections.frontpage.empty? %} From 0147832a64dc237feda2affa6b61127398e05b44 Mon Sep 17 00:00:00 2001 From: Adam Hollett Date: Fri, 16 Oct 2015 10:53:23 -0400 Subject: [PATCH 3/3] Polishing up boolean logic and types --- _basics/truthy-and-falsy.md | 79 +++++++++++++------------------------ _basics/types.md | 79 ++++++++++++++----------------------- 2 files changed, 57 insertions(+), 101 deletions(-) diff --git a/_basics/truthy-and-falsy.md b/_basics/truthy-and-falsy.md index bbacdc7..dddee83 100644 --- a/_basics/truthy-and-falsy.md +++ b/_basics/truthy-and-falsy.md @@ -2,77 +2,53 @@ 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' %} -{% if tobi %} -This will always be true. + +{% if tobi == true %} + This condition will always be true. {% endif %} {% 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:

Input

-{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} {% if settings.fp_heading %} -

{{ settings.fp_heading }}

+

{{ settings.fp_heading }}

{% endif %} {% endraw %}{% endhighlight %} -

Output

{% highlight html %}{% raw %}

{% endraw %}{% endhighlight %} -To avoid this, you can check to see if the string is blank, as follows: - -
-{% highlight html %}{% raw %} -{% unless settings.fp_heading == blank %} -

{{ settings.fp_heading }}

-{% endunless %} -{% endraw %}{% endhighlight %} -
- -
- -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>: +[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 `
`:

Input

{% highlight html %}{% raw %} {% if pages[settings.page] %} -
{{ pages[settings.page].content }}
+
{{ pages[settings.page].content }}
{% endif %} {% endraw %}{% endhighlight %} -

Output

{% highlight html %}{% raw %}
{% endraw %}{% endhighlight %} +## Falsy -## What is falsy? - -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 nil. Since that is “falsy”, you can do this: - -{% highlight html %}{% raw %} -{% if collection.image %} - -{% endif %} -{% endraw %}{% endhighlight %} - -The value false is returned through many Liquid object properties such as product.available. +The falsy values in Liquid are [nil](/basics/types/#nil) and [false](/basics/types/#boolean). ## Summary @@ -80,16 +56,15 @@ The table below summarizes what is truthy or falsy in Liquid. | | truthy | falsy | | ------------- |:-------------:|:-------------:| -| true | × | | -| false | | × | -| nil | | × | -| string | × | | -| empty string | × | | -| 0 | × | | -| 1 or 2 or 3.14 | × | | -| array | × | | -| empty array | × | | -| collection | × | | -| collection with no products | × | | -| page | × | | -| EmptyDrop | × | | +| true | • | | +| false | | • | +| nil | | • | +| string | • | | +| empty string | • | | +| 0 | • | | +| integer | • | | +| float | • | | +| array | • | | +| empty array | • | | +| page | • | | +| EmptyDrop | • | | diff --git a/_basics/types.md b/_basics/types.md index da10c96..711b8a8 100644 --- a/_basics/types.md +++ b/_basics/types.md @@ -2,14 +2,14 @@ title: Types --- -Liquid objects can return one of six types: +Liquid objects can have one of six types: - [string](#string) - [number](#number) -- boolean -- nil -- array -- EmptyDrop +- [boolean](#boolean) +- [nil](#nil) +- [array](#array) +- [EmptyDrop](#emptydrop) 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 %} {% endhighlight %} -### Booleans +### 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.

Input

-{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} The current user is {{ user.name }} {% endraw %}{% endhighlight %}

Output

-{% highlight html %}{% raw %} +{% highlight text %}{% raw %} The current user is {% endraw %}{% endhighlight %} -### Arrays +### Array Arrays hold lists of variables of any type. #### 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.

Input

-{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} {% for user in site.users %} {{ user }} @@ -92,17 +92,17 @@ To access items in an array, you can loop through each item in the array using a {% endraw %}{% endhighlight %}

Output

-{% highlight html %}{% raw %} +{% highlight text %}{% raw %} Tobi Lina Tetsuro Adam {% 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.

Input

-{% highlight html %}{% raw %} +{% highlight liquid %}{% raw %} {{ site.users[0] }} {{ site.users[1] }} @@ -110,67 +110,48 @@ You can use square bracket `[ ]` notation to access a specific item in an array. {% endraw %}{% endhighlight %}

Output

-{% highlight html %}{% raw %} +{% highlight text %}{% raw %} Tobi Lina Adam {% 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 -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 page_1 = pages[variable] %} -{% assign page_2 = pages["i-do-not-exist-in-your-store"] %} -{% assign page_3 = pages.this-handle-does-not-belong-to-any-page %} +{% assign page_2 = pages["does-not-exist"] %} +{% assign page_3 = pages.this-handle-does-not-exist %} {% endraw %}{% endhighlight %} -EmptyDrop objects only have one attribute, empty?, 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 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. +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 empty? 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 %} -{% unless pages.frontpage.empty? %} - +{% highlight liquid %}{% raw %} +{% unless pages.about.empty? %} +

{{ pages.frontpage.title }}

{{ pages.frontpage.content }}
{% endunless %} {% 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 %}

{% 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 %} -

We do have a 'frontpage' collection but it's empty.

- {% endfor %} -{% endunless %} -{% endraw %}{% endhighlight %} - - - - - - -