Split Description into Three Sections

Hi all,

Im trying to have 3 sections on my product description. What's the best solution for this? I usually use:

{{ product.description | split: '<!-- split -->' | first }}
{{ product.description | split: '<!-- split -->' | last }}


But that only seems to work for 2 sections. Anyone?


Thanks

 

0 Likes
Jason
Shopify Expert
10407 163 2046

Hey Matt,

By using split you'll create an array so you can call each item by their index. For example:

{% assign foo = 'a,b,c' | split:',' %}
1: {{ foo[0] }}
2: {{ foo[1] }}
3: {{ foo[2] }}

You'll want to add some conditional logic to make sure each section isn't blank (or false) before use.

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
Martynas
Tourist
18 0 2

Hi Jason,

To create conditional logic would you use for loop or if statment?  How would it look like in this case? 

I can't figure it out :)

 

Cheers

 

 

Bra_mo
Excursionist
22 0 4

Hi @Jason - the super veteran of the forum!

After posting about a similar issue (slightly more complex) I found your solution here about looping through it.

However, I struggled to implement it at all and I'm not sure if this solution actually applies.

 

https://community.shopify.com/c/Technical-Q-A/Split-Product-Description-into-X-parts-tricky-one/td-p...

 

In case, you have an idea I would really appreciate your input! Thanks in advance!

 

Best,
Moritz

0 Likes