mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
A few copy-edits
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
title: Introduction
|
||||
---
|
||||
|
||||
Liquid code can be categorized into three parts: **objects**, **tags**, and **filters**.
|
||||
Liquid code can be categorized into [**objects**](#objects), [**tags**](#tags), and [**filters**](#filters).
|
||||
|
||||
## Objects
|
||||
|
||||
@@ -23,9 +23,9 @@ In this case, Liquid is rendering the content of an object called `page.title`,
|
||||
|
||||
## Tags
|
||||
|
||||
**Tags** create the logic and control flow for your templates. They are denoted by curly braces and percent signs: `{% raw %}{%{% endraw %}` and `{% raw %}%}{% endraw %}`.
|
||||
**Tags** create the logic and control flow for templates. They are denoted by curly braces and percent signs: `{% raw %}{%{% endraw %}` and `{% raw %}%}{% endraw %}`.
|
||||
|
||||
Tag markup does not resolve to text. This means that you can assign variables and create conditionals and loops without showing any of the Liquid logic on the page.
|
||||
The markup used in tags does not produce any visible text. This means that you can assign variables and create conditions and loops without showing any of the Liquid logic on the page.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -51,7 +51,7 @@ You can read more about each type of tag in their respective sections.
|
||||
|
||||
## Filters
|
||||
|
||||
**Filters** modify the output of a Liquid object. They are using within an output and are separated by a `|`.
|
||||
**Filters** change the output of a Liquid object. They are using within an output and are separated by a `|`.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ title: Operators
|
||||
|
||||
Liquid includes many logical and comparison operators.
|
||||
|
||||
## Basic Operators
|
||||
## Basic operators
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
|
||||
@@ -12,11 +12,11 @@ In programming, anything that returns `true` in a conditional is called **truthy
|
||||
|
||||
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 string "Tobi" is not a boolean, but it is truthy in a conditional:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{% assign tobi = 'Tobi' %}
|
||||
{% assign tobi = "Tobi" %}
|
||||
|
||||
{% if tobi == true %}
|
||||
This condition will always be true.
|
||||
@@ -38,7 +38,7 @@ In the example below, the text "Tobi" is not a boolean, but it is truthy in a co
|
||||
<h1></h1>
|
||||
```
|
||||
|
||||
[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:
|
||||
[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`:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -54,7 +54,7 @@ In the example below, the text "Tobi" is not a boolean, but it is truthy in a co
|
||||
|
||||
## Falsy
|
||||
|
||||
The falsy values in Liquid are [nil](/basics/types/#nil) and [false](/basics/types/#boolean).
|
||||
The falsy values in Liquid are [`nil`](/basics/types/#nil) and [`false`](/basics/types/#boolean).
|
||||
|
||||
## Summary
|
||||
|
||||
|
||||
+11
-11
@@ -11,11 +11,11 @@ Liquid objects can have one of six types:
|
||||
- [Array](#array)
|
||||
- [EmptyDrop](#emptydrop)
|
||||
|
||||
Liquid variables can be initialized by using the [assign](/tags/#assign) or [capture](/tags/#capture) tags.
|
||||
You can initialize Liquid variables with the [assign](/tags/#assign) or [capture](/tags/#capture) tags.
|
||||
|
||||
## String
|
||||
|
||||
Strings are declared by wrapping a variable's value in single or double quotes.
|
||||
Declare a string by wrapping a variable's value in single or double quotes:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -25,7 +25,7 @@ Strings are declared by wrapping a variable's value in single or double quotes.
|
||||
|
||||
## Number
|
||||
|
||||
Numbers include floats and integers.
|
||||
Numbers include floats and integers:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -36,7 +36,7 @@ Numbers include floats and integers.
|
||||
|
||||
## 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:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -49,7 +49,7 @@ Booleans are either `true` or `false`. No quotations are necessary when declarin
|
||||
|
||||
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 the truthfulness of a statement.
|
||||
Nil is [treated as false](/basics/truthy-and-falsy) 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:
|
||||
|
||||
@@ -79,7 +79,7 @@ Arrays hold lists of variables of any type.
|
||||
|
||||
### Accessing items in arrays
|
||||
|
||||
To access all of the items in an array, you can loop through each item in the array using an [iteration tag](/tags/iteration/).
|
||||
To access all the items in an array, you can loop through each item in the array using an [iteration tag](/tags/iteration/).
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -117,13 +117,13 @@ Adam
|
||||
|
||||
### Initializing arrays
|
||||
|
||||
You cannot initialize arrays using pure Liquid.
|
||||
You cannot initialize arrays using only Liquid.
|
||||
|
||||
You can, however, 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 string into an array of substrings.
|
||||
|
||||
## EmptyDrop
|
||||
|
||||
An EmptyDrop object is returned if you try to access a deleted object (such as a page or post) by its handle. 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 by name. In the example below, `page_1`, `page_2` and `page_3` are all EmptyDrop objects.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -136,7 +136,7 @@ An EmptyDrop object is returned if you try to access a deleted object (such as a
|
||||
|
||||
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`.
|
||||
|
||||
### Checking for emptiness
|
||||
|
||||
@@ -152,7 +152,7 @@ Using the `empty?` attribute, you can check to see if an object exists or not be
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
If you don't check for emptiness first, Liquid may print empty HTML elements to the page:
|
||||
If you don't check for emptiness first, Liquid might print empty HTML elements to the page:
|
||||
|
||||
```html
|
||||
<h1></h1>
|
||||
|
||||
@@ -14,7 +14,7 @@ Makes the first character of a string capitalized.
|
||||
Title
|
||||
```
|
||||
|
||||
`capitalize` only capitalizes the first character of the string, so subsequent words are not affected:
|
||||
`capitalize` only capitalizes the first character of the string, so later words are not affected:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
+16
-4
@@ -2,7 +2,7 @@
|
||||
title: ceil
|
||||
---
|
||||
|
||||
`ceil` rounds the input up to the nearest whole number. The filter will attempt to cast any input to a number before it is applied.
|
||||
Rounds the input up to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -11,7 +11,7 @@ title: ceil
|
||||
```
|
||||
|
||||
```text
|
||||
2
|
||||
{{ 1.2 | ceil }}
|
||||
```
|
||||
|
||||
```liquid
|
||||
@@ -21,7 +21,7 @@ title: ceil
|
||||
```
|
||||
|
||||
```text
|
||||
2
|
||||
{{ 2.0 | ceil }}
|
||||
```
|
||||
|
||||
```liquid
|
||||
@@ -31,5 +31,17 @@ title: ceil
|
||||
```
|
||||
|
||||
```text
|
||||
184
|
||||
{{ 183.357 | ceil }}
|
||||
```
|
||||
|
||||
Here the input value is a string:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{{ "3.5" | ceil }}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
{{ "3.5" | ceil }}
|
||||
```
|
||||
|
||||
@@ -23,3 +23,15 @@ Fri, Jul 17, 15
|
||||
```text
|
||||
2015
|
||||
```
|
||||
|
||||
`date` works on strings if they contain well-formatted dates:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{{ "March 14, 2016" | date: "%b %d, %y" }}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
{{ "March 14, 2016" | date: "%b %d, %y" }}
|
||||
```
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
title: divided_by
|
||||
---
|
||||
|
||||
Divides a number by its argument.
|
||||
Divides a number by the specified number.
|
||||
|
||||
The result is rounded down to the nearest integer (that is, the [floor](/filters/floor)).
|
||||
|
||||
|
||||
+13
-1
@@ -2,7 +2,7 @@
|
||||
title: floor
|
||||
---
|
||||
|
||||
Rounds a number down to the nearest whole number.
|
||||
Rounds a number down to the nearest whole number. Liquid tries to convert the input to a number before the filter is applied.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -33,3 +33,15 @@ Rounds a number down to the nearest whole number.
|
||||
```text
|
||||
{{ 183.357 | floor }}
|
||||
```
|
||||
|
||||
Here the input value is a string:
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{{ "3.5" | floor }}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
{{ "3.5" | floor }}
|
||||
```
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
title: lstrip
|
||||
---
|
||||
|
||||
Removes all whitespaces (tabs, spaces, and newlines) from a string.
|
||||
Removes all whitespaces (tabs, spaces, and newlines) from the beginning of a string. The filter does not affect spaces between words.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
title: plus
|
||||
---
|
||||
|
||||
Adds a number with another number.
|
||||
Adds a number to another number.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
title: prepend
|
||||
---
|
||||
|
||||
Concatenates a string to the beginning of a string.
|
||||
Adds the specified string to the beginning of another string.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
title: remove
|
||||
---
|
||||
|
||||
Removes every occurrence of an argument from a string.
|
||||
Removes every occurrence of the specified substring from a string.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
title: remove_first
|
||||
---
|
||||
|
||||
Removes only the first occurrence of an argument from a string.
|
||||
Removes only the first occurrence of the specified substring from a string.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
@@ -2,14 +2,16 @@
|
||||
title: replace_first
|
||||
---
|
||||
|
||||
Replaces only the first occurrence of an argument in a string with the second argument.
|
||||
Replaces only the first occurrence of the first argument in a string with the second argument.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{{ "Take my protein pills and put my helmet on" | replace_first: "my", "your" }}
|
||||
{% assign my_string = "Take my protein pills and put my helmet on" %}
|
||||
{{ my_string | replace_first: "my", "your" }}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
{{ "Take my protein pills and put my helmet on" | replace_first: "my", "your" }}
|
||||
{% assign my_string = "Take my protein pills and put my helmet on" %}
|
||||
{{ my_string | replace_first: "my", "your" }}
|
||||
```
|
||||
|
||||
@@ -4,13 +4,12 @@ title: strip_html
|
||||
|
||||
Removes any HTML tags from a string.
|
||||
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{{ "Have <em>you</em> read <strong>James & the Giant Peach</strong>?" | strip_html }}
|
||||
{{ "Have <em>you</em> read <strong>Ulysses</strong>?" | strip_html }}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
{{ "Have <em>you</em> read <strong>James & the Giant Peach</strong>?" | strip_html }}
|
||||
{{ "Have <em>you</em> read <strong>Ulysses</strong>?" | strip_html }}
|
||||
```
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
title: truncatewords
|
||||
---
|
||||
|
||||
Shortens a string down to the number of words passed as the argument. If the number of words specified is less than the number of words in the string, an ellipsis (...) is appended to the string.
|
||||
Shortens a string down to the number of words passed as the argument. If the specified number of words is less than the number of words in the string, an ellipsis (...) is appended to the string.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
+2
-2
@@ -6,14 +6,14 @@ Removes any duplicate elements in an array.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{% assign my_array = "apples, oranges, bananas, oranges, apples" | split: ", " %}
|
||||
{% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " %}
|
||||
|
||||
{{ my_array | uniq | join: ", " }}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
{% assign my_array = "apples, oranges, bananas, oranges, apples" | split: ", " %}
|
||||
{% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " %}
|
||||
|
||||
{{ my_array | uniq | join: ", " }}
|
||||
```
|
||||
|
||||
+23
-23
@@ -28,7 +28,7 @@ This is a cake
|
||||
|
||||
## if
|
||||
|
||||
Executes a block of code only if a certain condition is met.
|
||||
Executes a block of code only if a certain condition is `true`.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -42,30 +42,9 @@ Executes a block of code only if a certain condition is met.
|
||||
These shoes are awesome!
|
||||
```
|
||||
|
||||
## elsif / else
|
||||
|
||||
Adds more conditions within an `if` or `unless` block.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
<!-- If customer.name = 'anonymous' -->
|
||||
{% if customer.name == 'kevin' %}
|
||||
Hey Kevin!
|
||||
{% elsif customer.name == 'anonymous' %}
|
||||
Hey Anonymous!
|
||||
{% else %}
|
||||
Hi Stranger!
|
||||
{% endif %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
Hey Anonymous!
|
||||
```
|
||||
|
||||
## unless
|
||||
|
||||
Similar to `if`, but executes a block of code only if a certain condition is **not** met.
|
||||
The opposite of `if` – executes a block of code only if a certain condition is **not** met.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -88,3 +67,24 @@ This would be the equivalent of doing the following:
|
||||
{% endif %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
## elsif / else
|
||||
|
||||
Adds more conditions within an `if` or `unless` block.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
<!-- If customer.name = 'anonymous' -->
|
||||
{% if customer.name == 'kevin' %}
|
||||
Hey Kevin!
|
||||
{% elsif customer.name == 'anonymous' %}
|
||||
Hey Anonymous!
|
||||
{% else %}
|
||||
Hi Stranger!
|
||||
{% endif %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
Hey Anonymous!
|
||||
```
|
||||
|
||||
+18
-20
@@ -2,13 +2,11 @@
|
||||
title: Iteration
|
||||
---
|
||||
|
||||
Iteration Tags are used to run a block of code repeatedly.
|
||||
Iteration tags run blocks of code repeatedly.
|
||||
|
||||
## for
|
||||
|
||||
Repeatedly executes a block of code. For a full list of attributes available within a `for` loop, see [forloop (object)](/themes/liquid-documentation/objects/for-loops).
|
||||
|
||||
`for` loops can output a maximum of 50 results per page. In cases where there are more than 50 results, use the [paginate](/themes/liquid-documentation/tags/theme-tags/#paginate) tag to split them across multiple pages.
|
||||
Repeatedly executes a block of code. For a full list of attributes available within a `for` loop, see [forloop (object)](https://docs.shopify.com/themes/liquid/objects/for-loops).
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -66,7 +64,7 @@ Causes the loop to skip the current iteration when it encounters the `continue`
|
||||
|
||||
### limit
|
||||
|
||||
Exits the for loop at a specific index.
|
||||
Limits the loop to the specified number of iterations.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -83,7 +81,7 @@ Exits the for loop at a specific index.
|
||||
|
||||
### offset
|
||||
|
||||
Starts the for loop at a specific index.
|
||||
Begins the loop at the specified index.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -103,26 +101,26 @@ Starts the for loop at a specific index.
|
||||
Defines a range of numbers to loop through. The range can be defined by both literal and variable numbers.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{% assign num = 4 %}
|
||||
{% for i in (1..num) %}
|
||||
{% for i in (3..5) %}
|
||||
{{ i }}
|
||||
{% endfor %}
|
||||
|
||||
{% for i in (3..5) %}
|
||||
{% raw %}
|
||||
{% assign num = 4 %}
|
||||
{% for i in (1..num) %}
|
||||
{{ i }}
|
||||
{% endfor %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
1 2 3 4
|
||||
3 4 5
|
||||
1 2 3 4
|
||||
```
|
||||
|
||||
### reversed
|
||||
|
||||
Reverses the order of the for loop.
|
||||
Reverses the order of the loop.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -209,7 +207,7 @@ Generates an HTML table. Must be wrapped in opening `<table>` and closing `</tab
|
||||
|
||||
## tablerow (parameters)
|
||||
|
||||
#### cols
|
||||
### cols
|
||||
|
||||
Defines how many columns the tables should have.
|
||||
|
||||
@@ -237,15 +235,15 @@ Defines how many columns the tables should have.
|
||||
</td>
|
||||
<td class="col2">
|
||||
Bullseye Shirt
|
||||
</td>
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="row3">
|
||||
<td class="col1">
|
||||
Another Classic Vinyl
|
||||
</td>
|
||||
Another Classic Vinyl
|
||||
</td>
|
||||
<td class="col2">
|
||||
Awesome Jeans
|
||||
</td>
|
||||
Awesome Jeans
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
```
|
||||
@@ -262,7 +260,7 @@ Exits the tablerow after a specific index.
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
#### offset
|
||||
### offset
|
||||
|
||||
Starts the tablerow after a specific index.
|
||||
|
||||
@@ -274,7 +272,7 @@ Starts the tablerow after a specific index.
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
#### range
|
||||
### range
|
||||
|
||||
Defines a range of numbers to loop through. The range can be defined by both literal and variable numbers.
|
||||
|
||||
|
||||
+9
-9
@@ -10,18 +10,18 @@ Creates a new variable.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{% assign my_variable = false %}
|
||||
{% if my_variable != true %}
|
||||
{% assign my_variable = false %}
|
||||
{% if my_variable != true %}
|
||||
This statement is valid.
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
```text
|
||||
This statement is valid.
|
||||
This statement is valid.
|
||||
```
|
||||
|
||||
Use quotations ("") to save the variable as a string.
|
||||
Wrap a variable in quotations `"` to save it as a string.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
@@ -55,9 +55,9 @@ Creates a new number variable, and increases its value by one every time it is c
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
{% increment variable %}
|
||||
{% increment variable %}
|
||||
{% increment variable %}
|
||||
{% increment my_counter %}
|
||||
{% increment my_counter %}
|
||||
{% increment my_counter %}
|
||||
{% endraw %}
|
||||
```
|
||||
|
||||
@@ -69,7 +69,7 @@ Creates a new number variable, and increases its value by one every time it is c
|
||||
|
||||
Variables created through the `increment` tag are independent from variables created through `assign` or `capture`.
|
||||
|
||||
In the example below, a variable named "var" is created through `assign`. The `increment` tag is then used several times on a variable with the same name. However, note that the `increment` tag does not affect the value of "var" that was created through `assign`.
|
||||
In the example below, a variable named "var" is created through `assign`. The `increment` tag is then used several times on a variable with the same name. Note that the `increment` tag does not affect the value of "var" that was created through `assign`.
|
||||
|
||||
```liquid
|
||||
{% raw %}
|
||||
|
||||
Reference in New Issue
Block a user