0

I am currently working with liquid templates and I have a string that has a pipe delimiter |. How would I be able to split this string and display the specific string one I want?

I figured out how to split the string:

{{ "Cookies | Biscuits" | split: "|" }}

But I am unsure where to add the index of the string I need. For example, how can I get "Biscuits"?

Thanks

mzrnsh
  • 2,260
  • 2
  • 20
  • 25
BLSG
  • 21
  • 5

1 Answers1

2

{{ "Cookies | Biscuits" | split: "|" | first | strip }} for Cookies

{{ "Cookies | Biscuits" | split: "|" | last | strip }} for Biscuits

or access it by index:

{%- assign typeParts = "Cookies | Biscuits" | split: "|" -%}

and then:
{{ typeParts[0] | strip }} for Cookies
{{ typeParts[1] | strip }} for Biscuits

Vladimir
  • 2,461
  • 2
  • 14
  • 18
  • The first part of your answer using "first" & "last" was actually a better solution then what my implementation was going to be. Thank you very much. – BLSG May 15 '20 at 12:31