Files
liquid/_basics/types.md
2015-08-28 15:45:28 -04:00

5.3 KiB
Raw Blame History

title
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.

Strings

Strings are declared by wrapping the variable's value in single or double quotes.

{% raw %} {% assign my_string = "Hello World!" %} {% endraw %}

Numbers

Numbers include floats and integers.

{% raw %} {% assign my_num = 25 %} {% endraw %}

Booleans

Booleans are either true or false. No quotations are necessary when declaring a boolean.

{% raw %} {% assign foo = true %} {% assign bar = false %} {% endraw %}

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 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.

{% raw %} {% if fulfillment.tracking_numbers %} We have a tracking number! {% endif %} {% endraw %}

Any tags or outputs that return nil will not show anything on the screen.

Input

{% highlight html %}{% raw %} Tracking number: {{ fulfillment.tracking_numbers }} {% endraw %}{% endhighlight %}

Output

{% highlight html %}{% raw %} Tracking number: {% endraw %}{% endhighlight %}

Arrays

Arrays hold a list of variables of all types.

Accessing all items in an array

To access items in an array, you can loop through each item in the array using a for tag or a tablerow tag.

Input

{% highlight html %}{% raw %} {% for tag in product.tags %} {{ tag }} {% endfor %} {% endraw %}{% endhighlight %}

Output

{% highlight html %}{% raw %} sale summer spring wholesale {% 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.

Input

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

Output

{% highlight html %}{% raw %} sale summer spring wholesale {% 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 %} <script> var cars = ["Saab", "Volvo", "BMW"]; </script> {% 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.

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. In the example below, page_1, page_2 and page_3 are all EmptyDrop objects.

{% highlight html %}{% 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 %} {% endraw %}{% endhighlight %}

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.

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.

{% highlight html %}{% raw %} {% unless pages.frontpage.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:

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