liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Clarify documentation of slice to maybe include arrays

Open Strangehill opened this issue 8 years ago • 4 comments

Documentation has 'slice' under string filters. I needed it to use it for an array. I tried it and it worked even though I couldn't find it mentioned in the documentation. Is this an oversight or an unintentional capability of the 'slice' filter?

Strangehill avatar Dec 26 '17 15:12 Strangehill

Slice includes array

arafatkatze avatar Dec 28 '17 07:12 arafatkatze

Stumbled upon this while looking if it was documented somewhere. That'd be cool to find it out in the docs indeed ❤️

simonc avatar Sep 18 '20 17:09 simonc

As @Arafatk noted, the slice filter has explicit support for arrays. Hopefully this can be added to the slice documentation so it's more than just a hidden feature.

timdmackey avatar Apr 13 '21 20:04 timdmackey

I was helping someone with an issue with slice, and it turns out that they were calling slice: 0 on an Array, and they were comparing the result to the first element of the Array, which returned false, and they didn't understand why.

I was surprised to find out that Liquid returns an Array that contains the first element of the previous Array, as opposed to just the first element itself.

In Ruby, calling .slice(0) on an Array returns the first element of that Array, so I was surprised as a Rubyist, but doubly surprised given Shopify's affinity for Ruby.

Was this intentional? If so, what's the reasoning? If it was intentional, it would be super helpful to mention that in the documentation.

Thanks!

monfresh avatar Mar 30 '25 02:03 monfresh