From 09e4fef7127c66aa23d8fa5c6af58be26aa78890 Mon Sep 17 00:00:00 2001 From: EricFromCanada Date: Mon, 22 Feb 2021 16:39:10 -0500 Subject: [PATCH] `slice` can be used with arrays --- _filters/slice.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/_filters/slice.md b/_filters/slice.md index eeb1bb8..43e8e7f 100644 --- a/_filters/slice.md +++ b/_filters/slice.md @@ -1,11 +1,11 @@ --- title: slice -description: Liquid filter that returns a substring from a given position in a string. +description: Liquid filter that returns a substring or item from a given position in a string or array. --- -Returns a substring of 1 character beginning at the index specified by the first argument. An optional second argument specifies the length of the substring to be returned. +Returns a substring of one character or series of array items beginning at the index specified by the first argument. An optional second argument specifies the length of the substring or number of array items to be returned. -String indices are numbered starting from 0. +String or array indices are numbered starting from 0.

Input

```liquid @@ -43,7 +43,23 @@ String indices are numbered starting from 0. {{ "Liquid" | slice: 2, 5 }} ``` -If the first argument is a negative number, the indices are counted from the end of the string: +Here the input value is an array: + +

Input

+```liquid +{% raw %} +{% assign beatles = "John, Paul, George, Ringo" | split: ", " %} +{{ beatles | slice: 1, 2 }} +{% endraw %} +``` + +

Output

+```text +{% assign beatles = "John, Paul, George, Ringo" | split: ", " %} +{{ beatles | slice: 1, 2 }} +``` + +If the first argument is a negative number, the indices are counted from the end of the string.

Input

```liquid