Splitting a description into seperate paragraphs

Shopify Partner
19 0 1

Hi there,

I have found posts on how to split strings into an array of words, but what I am after is a way to split a product.description into 2 or more sentences/paragraphs, so that I can position part of the text in a different area of the product page.

For example, if my client enters :

These boards are completely natural with no oils or coatings applied and fit in perfectly in either modern or rustic kitchens.

*

Dimensions
320 x 130 x 23mm.

Then I would like to be able to strip all of the copy before the * and place it into one variable. Then strip all the copy after the * and place it into another variable. Then I would have the freedom to position the text in separate places on my product page.

Is this possible? How can I achieve this please?

Cheers

0 Likes
Shopify Staff
Shopify Staff
489 0 32

Definitely! You can split up any string of text with the "split" filter (http://wiki.shopify.com/Split).

You can find another example here: http://www.shopify.com/technology/4461872-liquid-updates

Cheers,

Tetchi 

www.tetchi.ca | www.twitter.com/t3tchi
0 Likes
Tourist
20 0 1

Hi Nadia,

I just had the same need and did a bit of a search (of the pages Tetchi mentions) and this is what I ended up with to grab the first paragraph of the description and use it elsewhere...

 

{{ product.description | split: '</p>' | first }}

Hope this helps.

Cheers

Brett

1 Like
Shopify Staff
Shopify Staff
5704 0 287

It is a bad idea to strip closing paragraph tags, that'll produce a broken layout, especially in Internet Explorer. Use this delimiter instead:

<!-- split -->

Or any HTML comment.

Then:

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

Whatever you split on is removed.

Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com
1 Like
Tourist
20 0 1

Thanks Caroline. I'll do that and apologies for the misdirection Nadia.

0 Likes
Tourist
20 0 1

Caroline,

I've been thinking about this a little bit and I'm wondering if there is a way for my client to add the comment tag you use in your example without going into the html view? Or do I need them to go in and add the comment tag wherever they want the break to appear in the text?

0 Likes
Shopify Staff (Retired)
Shopify Staff (Retired)
5711 0 124

It will have to ba added using the HTML view. The alternate would be to use metafields for this, but that will require an app and may be more troublesome for the end user.

http://shopifyplus.com ::: http://twitter.com/bacchus
0 Likes
Shopify Partner
19 0 1

Thank you so much for your input everyone.

Worked perfectly.

0 Likes
Shopify Partner
19 0 1

Whoops - jumped the gun with that reply.

So it works perfectly when a description has a

<!-- split -->

But for descriptions that don't have one, when I say

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

It spits the whole description out again.

How can I do an If/Else to test whether a split exists?

Cheers again!

0 Likes
Shopify Staff
Shopify Staff
5704 0 287

Try this:

{% if product.description contains '<!-- split -->' %}
{{ product.description | split: '<!-- split -->' | last }}
{% endif %}
Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com
0 Likes