mirror of
https://github.com/kemko/liquid.git
synced 2026-01-06 10:15:40 +03:00
Updates to types and truthiness
This commit is contained in:
@@ -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 <tt>nil</tt> and <tt>false</tt>.
|
||||
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 <code>settings.fp_heading</code> 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:
|
||||
|
||||
<p class="input">Input</p>
|
||||
{% highlight html %}{% raw %}
|
||||
@@ -32,7 +32,7 @@ This will always be true.
|
||||
<h1></h1>
|
||||
{% endraw %}{% endhighlight %}
|
||||
|
||||
To avoid this, you can check to see if the string is <code>blank</code>, as follows:
|
||||
To avoid this, you can check to see if the string is <code>blank</code>, as follows:
|
||||
|
||||
<div>
|
||||
{% highlight html %}{% raw %}
|
||||
@@ -44,7 +44,7 @@ To avoid this, you can check to see if the string is <code>blank</code>, as foll
|
||||
|
||||
<hr/>
|
||||
|
||||
An [EmptyDrop](/themes/liquid-documentation/basics/types/#empty-drop) is also truthy. In the example below, if <code>settings.page</code> 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>:
|
||||
|
||||
<p class="input">Input</p>
|
||||
{% 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 <tt>nil</tt> and <tt>false</tt>.
|
||||
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:
|
||||
|
||||
@@ -76,7 +76,7 @@ The value <tt>false</tt> 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 | × | |
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
133
_basics/types.md
133
_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 <a href="/themes/liquid-documentation/tags/variable-tags/#assign">assign</a> or <a href="/themes/liquid-documentation/tags/variable-tags/#capture">capture</a> 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.
|
||||
|
||||
<div>
|
||||
{% highlight liquid %}
|
||||
{% raw %}
|
||||
{% assign my_string = "Hello World!" %}
|
||||
{% endraw %}
|
||||
</div>
|
||||
{% endhighlight %}
|
||||
|
||||
### Number
|
||||
|
||||
### Numbers
|
||||
Numbers include floats and integers.
|
||||
|
||||
Numbers include floats and integers.
|
||||
|
||||
<div>
|
||||
{% highlight liquid %}
|
||||
{% raw %}
|
||||
{% assign my_num = 25 %}
|
||||
{% assign my_int = 25 %}
|
||||
{% assign my_float = 39.756 %}
|
||||
{% endraw %}
|
||||
</div>
|
||||
|
||||
|
||||
{% 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.
|
||||
|
||||
<div>
|
||||
{% highlight liquid %}
|
||||
{% raw %}
|
||||
{% assign foo = true %}
|
||||
{% assign bar = false %}
|
||||
{% endraw %}
|
||||
</div>
|
||||
|
||||
|
||||
{% 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.
|
||||
|
||||
<p class="input">Input</p>
|
||||
|
||||
{% highlight html %}{% raw %}
|
||||
Tracking number: {{ fulfillment.tracking_numbers }}
|
||||
The current user is {{ user.name }}
|
||||
{% endraw %}{% endhighlight %}
|
||||
|
||||
<p class="output">Output</p>
|
||||
|
||||
<div>
|
||||
{% highlight html %}{% raw %}
|
||||
Tracking number:
|
||||
The current user is
|
||||
{% endraw %}{% endhighlight %}
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
### 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 <a href="/themes/liquid-documentation/tags/#for">for</a> tag or a <a href="/themes/liquid-documentation/tags/#tablerow">tablerow</a> 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.
|
||||
|
||||
<p class="input">Input</p>
|
||||
<div>
|
||||
{% highlight html %}{% raw %}
|
||||
<!-- if product.tags = "sale", "summer", "spring", "wholesale" -->
|
||||
{% for tag in product.tags %}
|
||||
{{ tag }}
|
||||
<!-- if site.users = "Tobi", "Lina", "Tetsuro", "Adam" -->
|
||||
{% for user in site.users %}
|
||||
{{ user }}
|
||||
{% endfor %}
|
||||
{% endraw %}{% endhighlight %}
|
||||
</div>
|
||||
|
||||
<p class="output">Output</p>
|
||||
<div>
|
||||
{% highlight html %}{% raw %}
|
||||
sale summer spring wholesale
|
||||
Tobi Lina Tetsuro Adam
|
||||
{% endraw %}{% endhighlight %}
|
||||
</div>
|
||||
|
||||
|
||||
#### 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.
|
||||
|
||||
<p class="input">Input</p>
|
||||
<div>
|
||||
{% highlight html %}{% raw %}
|
||||
<!-- if product.tags = "sale", "summer", "spring", "wholesale" -->
|
||||
{{ product.tags[0] }}
|
||||
{{ product.tags[1] }}
|
||||
{{ product.tags[2] }}
|
||||
{{ product.tags[3] }}
|
||||
<!-- if site.users = "Tobi", "Lina", "Tetsuro", "Adam" -->
|
||||
{{ site.users[0] }}
|
||||
{{ site.users[1] }}
|
||||
{{ site.users[3] }}
|
||||
{% endraw %}{% endhighlight %}
|
||||
</div>
|
||||
|
||||
<p class="output">Output</p>
|
||||
<div>
|
||||
{% highlight html %}{% raw %}
|
||||
sale
|
||||
summer
|
||||
spring
|
||||
wholesale
|
||||
Tobi
|
||||
Lina
|
||||
Adam
|
||||
{% endraw %}{% endhighlight %}
|
||||
</div>
|
||||
|
||||
|
||||
#### Initializing an array
|
||||
|
||||
It is not possible to initialize an array in Liquid. For example, in Javascript you could do something like this:
|
||||
|
||||
<div>
|
||||
{% highlight html %}{% raw %}
|
||||
<script>
|
||||
var cars = ["Saab", "Volvo", "BMW"];
|
||||
</script>
|
||||
{% endraw %}{% endhighlight %}
|
||||
</div>
|
||||
|
||||
In Liquid, you must instead use the <code>split</code> filter to break a single string into an array of substrings. See <a href="/themes/liquid-documentation/filters/string-filters/#split">here</a> 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, <code>page_1</code>, <code>page_2</code> and <code>page_3</code> 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, <code>empty?</code>, which is always true.
|
||||
EmptyDrop objects only have one attribute, <code>empty?</code>, 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 <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>.
|
||||
|
||||
#### Applications in themes
|
||||
|
||||
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 <code>empty?</code> 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 <code>empty?</code> 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 %}
|
||||
<h1></h1>
|
||||
<div></div>
|
||||
{% 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? %}
|
||||
|
||||
Reference in New Issue
Block a user