New content for nearly all filters

This commit is contained in:
Adam Hollett
2015-12-13 22:39:26 -05:00
parent f49b990e94
commit 1579545be2
41 changed files with 766 additions and 294 deletions

View File

@@ -1,11 +0,0 @@
---
layout: default
---
{{ content }}
{% for item in page.examples %}
<code>{{ item }}</code>
{% endfor %}

View File

@@ -21,7 +21,8 @@
box-sizing: border-box;
}
& + .highlight {
// Label every second code block with "Output"
& + .highlight:nth-of-type(2n) {
&:before {
content: "Output";
}

View File

@@ -6,12 +6,12 @@ title: append
```liquid
{% raw %}
{{ "/my/fancy/url" | append:".html" }}
{{ "/my/fancy/url" | append: ".html" }}
{% endraw %}
```
```text
/my/fancy/url.html
{{ "/my/fancy/url" | append: ".html" }}
```
`append` can also be used with variables:
@@ -19,12 +19,11 @@ title: append
```liquid
{% raw %}
{% assign filename = "/index.html" %}
{{ product.url | append: filename }}
{{ "website.com" | append: filename }}
{% endraw %}
```
```liquid
{% raw %}
{{ product.url }}/index.html
{% endraw %}
```text
{% assign filename = "/index.html" %}
{{ "website.com" | append: filename }}
```

View File

@@ -1,17 +1,35 @@
---
title: ceil
layout: method
examples:
- 1.2
---
`ceil` rounds the input up to the nearest whole number.
`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.
| Input | Output |
|:-------------------------------------------|:-------|
| {% raw %}`{{ 1.2 | ceil }}` {% endraw %} | 2 |
| {% raw %}`{{ 1.7 | ceil }}` {% endraw %} | 2 |
| {% raw %}`{{ 2.0 | ceil }}` {% endraw %} | 2 |
| {% raw %}`{{ "18.3" | ceil }}`{% endraw %} | 19 |
```liquid
{% raw %}
{{ 1.2 | ceil }}
{% endraw %}
```
It will attempt to cast any input to a number.
```text
2
```
```liquid
{% raw %}
{{ 2.0 | ceil }}
{% endraw %}
```
```text
2
```
```liquid
{% raw %}
{{ 183.357 | ceil }}
{% endraw %}
```
```text
184
```

View File

@@ -2,11 +2,24 @@
title: date
---
`date` converts a timestamp into another date format.
`date` converts a timestamp into another date format. The format for this syntax is the same as [`strftime`](//strftime.net).
| Input | Output |
|:--------------------------------------------------------------------------|:---------------------|
| {% raw %}`{{ article.published_at | date: "%a, %b %d, %y" }}`{% endraw %} | Tue, Apr 22, 14 |
| {% raw %}`{{ article.published_at | date: "%Y" }}`{% endraw %} | 2014 |
```liquid
{% raw %}
{{ article.published_at | date: "%a, %b %d, %y" }}
{% endraw %}
```
The format for this syntax is the same as [`strftime`](http://strftime.net/).
```text
Fri, Jul 17, 15
```
```liquid
{% raw %}
{{ article.published_at | date: "%Y" }}
{% endraw %}
```
```text
2015
```

View File

@@ -21,7 +21,7 @@ In this example, `product_price` is defined, so the default value is not used.
```liquid
{% raw %}
{% assign product_price = 4.99 %}
{{ product_price | default:2.99 }}
{{ product_price | default: 2.99 }}
{% endraw %}
```

View File

@@ -2,11 +2,36 @@
title: divided_by
---
This filter divides its input by its argument.
`divided_by` divides its input (left side) by its argument (right side). It uses `to_number`, which converts to a decimal value unless already a numeric.
| Code | Output |
|:--------------------------------------------------|:-------|
| {% raw %}`{{ 4 | divided_by: 2 }}` {% endraw %} | 2 |
| {% raw %}`{{ "16" | divided_by: 4 }}`{% endraw %} | 4 |
The result is rounded down to the nearest integer (that is, the [floor](/filters/floor)).
It uses `to_number`, which converts to a decimal value unless already a numeric.
```liquid
{% raw %}
{{ 4 | divided_by: 2 }}
{% endraw %}
```
```text
{{ 4 | divided_by: 2 }}
```
```liquid
{% raw %}
{{ 16 | divided_by: 4 }}
{% endraw %}
```
```text
{{ 16 | divided_by: 4 }}
```
```liquid
{% raw %}
{{ 5 | divided_by: 3 }}
{% endraw %}
```
```text
{{ 5 | divided_by: 3 }}
```

View File

@@ -2,10 +2,24 @@
title: downcase
---
`downcase` makes each character in a string lowercase.
`downcase` makes each character in a string lowercase. It has no effect on strings which are already all lowercase.
| Code | Output |
|:-------------------------------------------------------|:-----------------|
| {% raw %}`{{ "Peter Parker" | downcase }}`{% endraw %} | `"peter parker"` |
```liquid
{% raw %}
{{ "Parker Moore" | downcase }}
{% endraw %}
```
It doesn't modify strings which are already entirely lowercase.
```text
{{ "Parker Moore" | downcase }}
```
```liquid
{% raw %}
{{ "apple" | downcase }}
{% endraw %}
```
```text
{{ "apple" | downcase }}
```

View File

@@ -2,11 +2,24 @@
title: escape
---
Escapes a string by replacing characters with escape sequences (so that the string can be used in a URI).
Escapes a string by replacing some characters with escape sequences (so that the string can be used in a URL, for example). It doesn't change strings that have nothing to escape.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ "Need tips? Ask a friend!" | escape }}`{% endraw %} | `"Need%20tips%3F%Ask%20a%20friend%21"` |
| {% raw %}`{{ "Nope" | escape }}`{% endraw %} | `"Nope"` |
```liquid
{% raw %}
{{ "Have you read 'James & the Giant Peach'?" | escape }}
{% endraw %}
```
It doesn't modify strings that have nothing to escape.
```text
{{ "Have you read 'James & the Giant Peach'?" | escape }}
```
```liquid
{% raw %}
{{ "Tetsuro Takara" | escape }}
{% endraw %}
```
```text
{{ "Tetsuro Takara" | escape }}
```

View File

@@ -2,12 +2,24 @@
title: escape_once
---
Escapes a string without affecting existing escaped entities.
Escapes a string without changing existing escaped entities. It doesn't change strings that have nothing to escape.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ "1 < 2 & 3" | escape_once }}`{% endraw %} | `"1 < 2 & 3"` |
| {% raw %}`{{ "<< Accept & Checkout" | escape_once }}`{% endraw %} | `"<< Accept & Checkout"` |
| {% raw %}`{{ "Nope" | escape_once }}`{% endraw %} | `"Nope"` |
```liquid
{% raw %}
{{ "1 < 2 & 3" | escape_once }}
{% endraw %}
```
It doesn't modify strings that have nothing to escape.
```text
{{ "1 < 2 & 3" | escape_once }}
```
```liquid
{% raw %}
{{ "1 &lt; 2 &amp; 3" | escape_once }}
{% endraw %}
```
```text
{{ "1 &lt; 2 &amp; 3" | escape_once }}
```

View File

@@ -2,9 +2,32 @@
title: first
---
Returns the first element of an array. For example, if you have an array called `product.tags` that resolves to: `["sale", "mens", "womens", "awesome"]`:
`first` returns the first element of an array without sorting the array.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ product.tags | first }}`{% endraw %} | `"sale"` |
```liquid
{% raw %}
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array.first }}
{% endraw %}
```
```text
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array.first }}
```
```liquid
{% raw %}
{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}
{{ my_array.first }}
{% endraw %}
```
```text
{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}
{{ my_array.first }}
```

View File

@@ -4,11 +4,32 @@ title: floor
`floor` rounds the input down to the nearest whole number.
| Input | Output |
|:-------------------------------------------|:-------|
| {% raw %}`{{ 1.2 | floor }}` {% endraw %} | 1 |
| {% raw %}`{{ 1.7 | floor }}` {% endraw %} | 1 |
| {% raw %}`{{ 2.0 | floor }}` {% endraw %} | 2 |
| {% raw %}`{{ "18.3" | floor }}`{% endraw %} | 18 |
```liquid
{% raw %}
{{ 1.2 | floor }}
{% endraw %}
```
It will attempt to cast any input to a number.
```text
{{ 1.2 | floor }}
```
```liquid
{% raw %}
{{ 2.0 | floor }}
{% endraw %}
```
```text
{{ 2.0 | floor }}
```
```liquid
{% raw %}
{{ 183.357 | floor }}
{% endraw %}
```
```text
{{ 183.357 | floor }}
```

View File

@@ -2,10 +2,18 @@
title: join
---
`join` joins the elements of an array, using the character you provide.
`join` combines the elements of an array into a single string using the string you provide as a separator.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ product.tags | join: ', ' }}`{% endraw %} | `"sale, mens, womens, awesome` |
```liquid
{% raw %}
{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
In the sample above, assume that `product.tags` resolves to: `["sale", "mens", "womens", "awesome"]`.
{{ beatles | join: " and " }}
{% endraw %}
```
```text
{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
{{ beatles | join: " and " }}
```

View File

@@ -2,10 +2,32 @@
title: last
---
Return the last element of an array.
`last` returns the last element of an array without sorting the array.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ product.tags | last }}`{% endraw %} | `"awesome"` |
```liquid
{% raw %}
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
In the sample above, assume that `product.tags` resolves to: `["sale", "mens", "womens", "awesome"]`.
{{ my_array.last }}
{% endraw %}
```
```text
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array.last }}
```
```liquid
{% raw %}
{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}
{{ my_array.last }}
{% endraw %}
```
```text
{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}
{{ my_array.last }}
```

View File

@@ -2,8 +2,14 @@
title: lstrip
---
Strips all whitespace (tabs, spaces, and newlines) from the left side of a string.
`lstrip` removes all whitespace (tabs, spaces, and newlines) from the left side of a string.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ ' too many spaces ' | lstrip }}`{% endraw %} | `"too many spaces "` |
```liquid
{% raw %}
{{ " So much room for activities! " | lstrip }}
{% endraw %}
```
```text
{{ " So much room for activities! " | lstrip }}
```

View File

@@ -2,8 +2,34 @@
title: minus
---
Subtracts two numbers.
`minus` subtracts the number passed in from the input number.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 100 | minus: 10 }}`{% endraw %} | `90` |
```liquid
{% raw %}
{{ 4 | minus: 2 }}
{% endraw %}
```
```text
{{ 4 | minus: 2 }}
```
```liquid
{% raw %}
{{ 16 | minus: 4 }}
{% endraw %}
```
```text
{{ 16 | minus: 4 }}
```
```liquid
{% raw %}
{{ 183.357 | minus: 12 }}
{% endraw %}
```
```text
{{ 183.357 | minus: 12 }}
```

View File

@@ -2,8 +2,34 @@
title: modulo
---
Performs a modulo operation, i.e. returns the remainder.
`modulo` returns the remainder when the input is divided by the passed-in parameter.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 3 | modulo: 2 }}`{% endraw %} | `1` |
```liquid
{% raw %}
{{ 3 | modulo: 2 }}
{% endraw %}
```
```text
{{ 3 | modulo: 2 }}
```
```liquid
{% raw %}
{{ 24 | modulo: 7 }}
{% endraw %}
```
```text
{{ 24 | modulo: 7 }}
```
```liquid
{% raw %}
{{ 183.357 | modulo: 12 }}
{% endraw %}
```
```text
{{ 183.357 | modulo: 12 }}
```

View File

@@ -2,8 +2,24 @@
title: newline_to_br
---
Replace every newline (`n`) with an HTML break (`<br>`).
Replace every newline (`\n`) with an HTML line break (`<br>`).
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ "hello\nthere" | newline_to_br }}`{% endraw %} | `hello<br/>there` |
```liquid
{% raw %}
{% capture string_with_newlines %}
Hello
there
{% endcapture %}
{{ string_with_newlines | newline_to_br }}
{% endraw %}
```
```html
{% capture string_with_newlines %}
Hello
there
{% endcapture %}
{{ string_with_newlines | newline_to_br }}
```

View File

@@ -2,8 +2,34 @@
title: plus
---
Adds two numbers.
`plus` adds the number passed in to the input number.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 100 | plus: 10 }}`{% endraw %} | `110` |
```liquid
{% raw %}
{{ 4 | plus: 2 }}
{% endraw %}
```
```text
{{ 4 | plus: 2 }}
```
```liquid
{% raw %}
{{ 16 | plus: 4 }}
{% endraw %}
```
```text
{{ 16 | plus: 4 }}
```
```liquid
{% raw %}
{{ 183.357 | plus: 12 }}
{% endraw %}
```
```text
{{ 183.357 | plus: 12 }}
```

View File

@@ -2,8 +2,30 @@
title: prepend
---
Prepends a string onto another.
`prepend` adds a string to the beginning of the input string.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 'world' | prepend: 'hello ' }}`{% endraw %} | `hello world` |
```liquid
{% raw %}
{{ "apples, oranges, and bananas" | prepend: "Some fruit: " }}
{% endraw %}
```
```text
{{ "apples, oranges, and bananas" | prepend: "Some fruit: " }}
```
You can also `prepend` variables:
```liquid
{% raw %}
{% assign url = "liquidmarkup.com" %}
{{ "/index.html" | prepend: url }}
{% endraw %}
```
```text
{% assign url = "liquidmarkup.com" %}
{{ "/index.html" | prepend: url }}
```

View File

@@ -2,8 +2,14 @@
title: remove
---
Removes every occurrence of a given string.
`remove` removes every occurrence of of the passed-in substring from the input string.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 'hello, hello world' | remove: 'hello' }}`{% endraw %} | `, world` |
```liquid
{% raw %}
{{ "I strained to see the train through the rain" | remove: "rain" }}
{% endraw %}
```
```text
{{ "I strained to see the train through the rain" | remove: "rain" }}
```

View File

@@ -2,8 +2,14 @@
title: remove_first
---
Removes the first occurrence of a given string.
`remove` removes the first occurrence of of the passed-in substring from the input string.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 'hello, hello world' | remove_first: 'hello' }}`{% endraw %} | `, hello world` |
```liquid
{% raw %}
{{ "I strained to see the train through the rain" | remove_first: "rain" }}
{% endraw %}
```
```text
{{ "I strained to see the train through the rain" | remove_first: "rain" }}
```

View File

@@ -2,8 +2,14 @@
title: replace
---
Replaces every occurrence of a given string.
`replace` replaces every occurrence of the first passed-in string in the input string with the second passed-in string.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 'hello, hello world' | replace: 'hello', 'goodbye' }}`{% endraw %} | `goodbye, goodbye world` |
```liquid
{% raw %}
{{ "Take my protein pills and put my helmet on" | replace: "my", "your" }}
{% endraw %}
```
```text
{{ "Take my protein pills and put my helmet on" | replace: "my", "your" }}
```

View File

@@ -2,8 +2,14 @@
title: replace_first
---
Replaces the first occurrence of a given string.
`replace_first` replaces the first occurrence of the first passed-in string in the input string with the second passed-in string.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 'hello, hello world' | replace_first: 'hello', 'goodbye' }}`{% endraw %} | `goodbye, hello world` |
```liquid
{% raw %}
{{ "Take my protein pills and put my helmet on" | replace_first: "my", "your" }}
{% endraw %}
```
```text
{{ "Take my protein pills and put my helmet on" | replace_first: "my", "your" }}
```

View File

@@ -2,14 +2,30 @@
title: reverse
---
Reverses the order of an array.
`reverse` reverses the order of the items in an array. `reverse` cannot reverse a string.
{% highlight liquid %}
```liquid
{% raw %}
{{ product.tags }}
// ['cool', 'sale', 'purple', 'awesome']
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ product.tags | reverse }}
// ['awesome', 'purple', 'sale', 'cool']
{{ my_array | reverse | join: ", " }}
{% endraw %}
{% endhighlight %}
```
```text
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array | reverse | join: ", " }}
```
`reverse` cannot be used directly on a string, but you can split a string into an array, reverse the array, and rejoin it by chaining together filters:
```liquid
{% raw %}
{{ "Ground control to Major Tom." | split: "" | reverse | join: "" }}
{% endraw %}
```
```text
{{ "Ground control to Major Tom." | split: "" | reverse | join: "" }}
```

View File

@@ -2,11 +2,34 @@
title: round
---
Rounds the output to the nearest integer or specified number of decimals.
`round` rounds an input number to the nearest integer or, if a number is specified as a parameter, to that number of decimal places.
| Input | Output |
|:-------------------------------------------|:-------|
| {% raw %}`{{ 4.6 | round }}` {% endraw %} | 5 |
| {% raw %}`{{ 4.3 | round }}` {% endraw %} | 4 |
| {% raw %}`{{ 4.5612 | round: 2 }}` {% endraw %} | 4.56 |
```liquid
{% raw %}
{{ 1.2 | round }}
{% endraw %}
```
```text
{{ 1.2 | round }}
```
```liquid
{% raw %}
{{ 2.7 | round }}
{% endraw %}
```
```text
{{ 2.7 | round }}
```
```liquid
{% raw %}
{{ 183.357 | round: 2 }}
{% endraw %}
```
```text
{{ 183.357 | round: 2 }}
```

View File

@@ -2,8 +2,14 @@
title: rstrip
---
Strips all whitespace (tabs, spaces, and newlines) from the right side of a string.
`rstrip` removes all whitespace (tabs, spaces, and newlines) from the right side of a string.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ ' too many spaces ' | rstrip }}`{% endraw %} | `"too many spaces "` |
```liquid
{% raw %}
{{ " So much room for activities! " | rstrip }}
{% endraw %}
```
```text
{{ " So much room for activities! " | rstrip }}
```

View File

@@ -2,20 +2,38 @@
title: size
---
<p>Returns the size of a string or an array.</p>
`size` returns the number of characters in a string or the number of items in an array. `size` can also be used with dot notation (for example, `{% raw %}{{ my_string.size }}{% endraw %}`). This allows you to use `size` inside other tags like conditionals.
```liquid
{% raw %}
{{ "Ground control to Major Tom." | size }}
{% endraw %}
```
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 'is this a 30 character string?' | size }}`{% endraw %} | `30` |
```text
{{ "Ground control to Major Tom." | size }}
```
```liquid
{% raw %}
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
`size` can be used in dot notation, in cases where it needs to be used inside a tag.
{{ my_array | size }}
{% endraw %}
```
<div>
{% highlight html %}{% raw %}
{% if collections.frontpage.products.size > 10 %}
There are more than 10 products in this collection!
```text
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array | size }}
```
Using dot notation:
```liquid
{% raw %}
{% if site.pages.size > 10 %}
This is a big website!
{% endif %}
{% endraw %}{% endhighlight %}
</div>
{% endraw %}
```

View File

@@ -2,19 +2,48 @@
title: slice
---
The <code>slice</code> filter returns a substring, starting at the specified index. An optional second parameter can be passed to specify the length of the substring. If no second parameter is given, a substring of one character will be returned.
`slice` returns a substring of 1 character beginning at the index specified by the parameter passed in. An optional second parameter specifies the length of the substring to be returned.
String indices are numbered starting from 0.
| Input | Output |
|:------------------------------------------------|:-------|
| {% raw %}`{{ "hello" | slice: 0 }}`{% endraw %} | h |
| {% raw %}`{{ "hello" | slice: 1 }}`{% endraw %} | e |
| {% raw %}`{{ "hello" | slice: 1, 3 }}`{% endraw %} | ell |
```liquid
{% raw %}
{{ "Liquid" | slice: 0 }}
{% endraw %}
```
```text
{{ "Liquid" | slice: 0 }}
```
If the passed index is negative, it is counted from the end of the string.
```liquid
{% raw %}
{{ "Liquid" | slice: 2 }}
{% endraw %}
```
| Input | Output |
|:------------------------------------------------|:-------|
| {% raw %}`{{ "hello" | slice: -3, 2 }}`{% endraw %} | ll |
```text
{{ "Liquid" | slice: 2 }}
```
```liquid
{% raw %}
{{ "Liquid" | slice: 2, 5 }}
{% endraw %}
```
```text
{{ "Liquid" | slice: 2, 5 }}
```
If the first parameter is a negative number, the indices are counted from the end of the string:
```liquid
{% raw %}
{{ "Liquid" | slice: -3, 2 }}
{% endraw %}
```
```text
{{ "Liquid" | slice: -3, 2 }}
```

View File

@@ -4,20 +4,16 @@ title: sort
Sorts items in an array by a property of an item in the array. The order of the sorted array is case-sensitive.
<p class="input">Input</p>
<div>
{% highlight html %}{% raw %}
<!-- products = "a", "b", "A", "B" -->
{% assign products = collection.products | sort: 'title' %}
{% for product in products %}
{{ product.title }}
{% endfor %}{% endraw %}{% endhighlight %}
</div>
```liquid
{% raw %}
{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}
<p class="output">Output</p>
<div>
{% highlight html %}{% raw %}
A B a b
{% endraw %}{% endhighlight %}
</div>
{{ my_array | sort | join: ", " }}
{% endraw %}
```
```text
{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}
{{ my_array | sort | join: ", " }}
```

View File

@@ -2,22 +2,22 @@
title: split
---
The `split` filter takes on a substring as a parameter. The substring is used as a delimiter to divide a string into an array. You can output different parts of an array using [array filters](/themes/liquid-documentation/filters/array-filters).
`split` divides an input string into an array using the passed-in substring as a separator. `split` is commonly used to convert comma-separated items from a string to an array.
<p class="input">Input</p>
{% highlight liquid %}{% raw %}
{% assign words = "Hi, how are you today?" | split: ' ' %}
```liquid
{% raw %}
{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
{% for word in words %}
{{ word }}
{% for member in beatles %}
{{ member }}
{% endfor %}
{% endraw %}{% endhighlight %}
{% endraw %}
```
<p class="output">Output</p>
{% highlight text %}
Hi,
how
are
you
today?
{% endhighlight %}
```text
{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
{% for member in beatles %}
{{ member }}
{% endfor %}
```

View File

@@ -2,8 +2,14 @@
title: strip
---
Strips all whitespace (tabs, spaces, and newlines) from a string.
`strip` removes all whitespace (tabs, spaces, and newlines) from both the left and right sides of a string. It does not affect spaces between words.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ ' too many spaces ' | strip }}`{% endraw %} | `"too many spaces"` |
```liquid
{% raw %}
{{ " So much room for activities! " | strip }}
{% endraw %}
```
```text
{{ " So much room for activities! " | strip }}
```

View File

@@ -2,10 +2,15 @@
title: strip_html
---
<p>Strips all HTML tags from a string.</p>
`strip_html` removes any HTML tags from a string.
<p class="input">Input</p>
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ "<h1>Hello</h1> World" | strip_html }}`{% endraw %} | `Hello World` |
```liquid
{% raw %}
{{ "Have <em>you</em> read <strong>James & the Giant Peach</strong>?" | strip_html }}
{% endraw %}
```
```text
{{ "Have <em>you</em> read <strong>James & the Giant Peach</strong>?" | strip_html }}
```

View File

@@ -2,4 +2,25 @@
title: strip_newlines
---
Removes any line breaks/newlines from a string.
`strip_newlines` removes any newline characters (line breaks) from a string.
```liquid
{% raw %}
{% capture string_with_newlines %}
Hello
there
{% endcapture %}
{{ string_with_newlines | strip_newlines }}
{% endraw %}
```
```html
{% capture string_with_newlines %}
Hello
there
{% endcapture %}
{{ string_with_newlines | strip_newlines }}
```

View File

@@ -2,8 +2,34 @@
title: times
---
Multiplies two numbers.
`times` multiplies its input by its argument.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 200 | times: 2 }}`{% endraw %} | `400` |
```liquid
{% raw %}
{{ 3 | times: 2 }}
{% endraw %}
```
```text
{{ 3 | times: 2 }}
```
```liquid
{% raw %}
{{ 24 | times: 7 }}
{% endraw %}
```
```text
{{ 24 | times: 7 }}
```
```liquid
{% raw %}
{{ 183.357 | times: 12 }}
{% endraw %}
```
```text
{{ 183.357 | times: 12 }}
```

View File

@@ -2,9 +2,14 @@
title: truncate
---
`truncate` shortens a string down to the number of characters passed as a parameter. If the number of characters specified is less than the length of the string, an ellipsis (...) is appended to the string and is included in the character count.
<p>Truncates a string down to 'x' characters, where x is the number passed as a parameter. An ellipsis (...) is appended to the string and is included in the character count.</p>
```liquid
{% raw %}
{{ "Ground control to Major Tom." | truncate: 20 }}
{% endraw %}
```
| Input | Output |
|:-------------------------------------------|:-------|
| {% raw %}`{{ "The cat came back the very next day" | truncate: 10 }}`{% endraw %} | "The cat..." |
```text
{{ "Ground control to Major Tom." | truncate: 20 }}
```

View File

@@ -2,9 +2,14 @@
title: truncatewords
---
`truncate` shortens a string down to the number of words passed as a parameter. If the number of words specified is less than the number of words in the string, an ellipsis (...) is appended to the string.
<p>Truncates a string down to 'x' words, where x is the number passed as a parameter. An ellipsis (...) is appended to the truncated string.</p>
```liquid
{% raw %}
{{ "Ground control to Major Tom." | truncatewords: 3 }}
{% endraw %}
```
| Input | Output |
|:-------------------------------------------|:-------|
| {% raw %}`{{ "The cat came back the very next day" | truncatewords: 4 }}`{% endraw %} | The cat came back...|
```text
{{ "Ground control to Major Tom." | truncatewords: 3 }}
```

View File

@@ -2,18 +2,18 @@
title: uniq
---
`uniq` removes any duplicate elements in an array.
<p>Removes any duplicate instances of an element in an array.</p>
```liquid
{% raw %}
{% assign my_array = "apples, oranges, bananas, oranges, apples" | split: ", " %}
<p class="input">Input</p>
<div>{% highlight html %}{% raw %}
{% assign fruits = "orange apple banana apple orange" %}
{{ fruits | split: ' ' | uniq | join: ' ' }}
{% endraw %}{% endhighlight %}</div>
<p class="output">Output</p>
<div>{% highlight html%}{% raw %}
orange apple banana
{% endraw %}{% endhighlight %}</div>
{{ my_array | uniq | join: ", " }}
{% endraw %}
```
```text
{% assign my_array = "apples, oranges, bananas, oranges, apples" | split: ", " %}
{{ my_array | uniq | join: ", " }}
```

View File

@@ -2,10 +2,24 @@
title: upcase
---
<p>Converts a string into uppercase.</p>
`upcase` makes each character in a string uppercase. It has no effect on strings which are already all uppercase.
```liquid
{% raw %}
{{ "Parker Moore" | upcase }}
{% endraw %}
```
| Input | Output |
|:-------------------------------------------|:-------|
| {% raw %}`{{ 'loud noises' | upcase }}`{% endraw %} | LOUD NOISES |
```text
{{ "Parker Moore" | upcase }}
```
```liquid
{% raw %}
{{ "APPLE" | upcase }}
{% endraw %}
```
```text
{{ "APPLE" | upcase }}
```

View File

@@ -2,8 +2,24 @@
title: url_encode
---
Converts any URL-unsafe characters in a string into percent-encoded characters.
`url_encode` converts any URL-unsafe characters in a string into percent-encoded characters.
| Code | Output |
|:-------------------------------------------------------|:-------------------|
| {% raw %}`{{ 'john@liquid.com' | url_encode }}`{% endraw %} | `john%40liquid.com` |
```liquid
{% raw %}
{{ "john@liquid.com" | url_encode }}
{% endraw %}
```
```text
{{ "john@liquid.com" | url_encode }}
```
```liquid
{% raw %}
{{ "Tetsuro Takara" | url_encode }}
{% endraw %}
```
```text
{{ "Tetsuro Takara" | url_encode }}
```

View File

@@ -2,126 +2,108 @@
title: Variable
---
Variable Tags are used to create new Liquid variables.
Variable tags create new Liquid variables.
## assign
<p>Creates a new variable.</p>
Creates a new variable.
<div class="code-block code-block--input">
{% highlight html %}{% raw %}
```liquid
{% raw %}
{% assign my_variable = false %}
{% if my_variable != true %}
This statement is valid.
{% endif %}
{% endraw %}{% endhighlight %}
</div>
{% endraw %}
```
<div class="code-block code-block--output">
{% highlight html %}{% raw %}
```text
This statement is valid.
{% endraw %}{% endhighlight %}
</div>
```
Use quotations ("") to save the variable as a string.
<div class="code-block code-block--input">
{% highlight html %}{% raw %}
```liquid
{% raw %}
{% assign foo = "bar" %}
{{ foo }}
{% endraw %}{% endhighlight %}
</div>
{% endraw %}
```
<div class="code-block code-block--output">
{% highlight html %}{% raw %}
```text
bar
{% endraw %}{% endhighlight %}
</div>
```
## capture
<p>Captures the string inside of the opening and closing tags and assigns it to a variable. Variables created through {&#37; capture &#37;} are strings.</p>
Captures the string inside of the opening and closing tags and assigns it to a variable. Variables created through `{% raw %}{% capture %}{% endraw %}` are strings.
<div class="code-block code-block--input">
{% highlight html %}{% raw %}
```liquid
{% raw %}
{% capture my_variable %}I am being captured.{% endcapture %}
{{ my_variable }}
{% endraw %}{% endhighlight %}
</div>
{% endraw %}
```
<div class="code-block code-block--output">
{% highlight html %}{% raw %}
```text
I am being captured.
{% endraw %}{% endhighlight %}
</div>
```
## increment
Creates a new number variable, and increases its value by one every time it is called. The initial value is 0.
Creates a new number variable, and increases its value by one every time it is called. The initial value is 0.
<div class="code-block code-block--input">
{% highlight html %}{% raw %}
```liquid
{% raw %}
{% increment variable %}
{% increment variable %}
{% increment variable %}
{% endraw %}{% endhighlight %}
</div>
{% endraw %}
```
<div class="code-block code-block--output">
{% highlight html %}{% raw %}
```text
0
1
2
{% endraw %}{% endhighlight %}
</div>
```
Variables created through the <code>increment</code> tag are independent from variables created through <code>assign</code> or <code>capture</code>.
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 <code>assign</code>. The <code>increment</code> tag is then used several times on a variable with the same name. However, note that the <code>increment</code> tag does not affect the value of "var" that was created through <code>assign</code>.
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`.
<div class="code-block code-block--input">
{% highlight html %}{% raw %}
```liquid
{% raw %}
{% assign var = 10 %}
{% increment var %}
{% increment var %}
{% increment var %}
{{ var }}
{% endraw %}{% endhighlight %}
</div>
{% endraw %}
```
<div class="code-block code-block--output">
{% highlight html %}{% raw %}
```text
0
1
2
10
{% endraw %}{% endhighlight %}
</div>
10
```
## decrement
Creates a new number variable, and decreases its value by one every time it is called. The initial value is -1.
Creates a new number variable, and decreases its value by one every time it is called. The initial value is -1.
<div class="code-block code-block--input">
{% highlight html %}{% raw %}
```liquid
{% raw %}
{% decrement variable %}
{% decrement variable %}
{% decrement variable %}
{% endraw %}{% endhighlight %}
</div>
{% endraw %}
```
<div class="code-block code-block--output">
{% highlight html %}{% raw %}
```text
-1
-2
-3
{% endraw %}{% endhighlight %}
</div>
Like <a href="#increment">increment</a>, variables declared inside <code>decrement</code> are independent from variables created through <code>assign</code> or <code>capture</code>.
```
Like [increment](#increment), variables declared inside `decrement` are independent from variables created through `assign` or `capture`.