Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a string by newline in Shopify?

Tags:

shopify

liquid

I have a field in my settings.html where I am expecting the user to input multiple paragraphs separated by two newline characters. I would like to split this string of input into an array of strings, each representing a paragraph.

I would like to do something like this:

{% assign paragraphs = settings.intro | split: '\n' %}
{% for paragraph in paragraphs %}
    <p>
        {{ paragraph }}
    </p>
{% endfor %}

I can't seem to figure out how to refer to the newline character in Liquid. How can I go about doing this? Is there some kind of work around?

like image 750
ProjectFrank Avatar asked Dec 29 '14 19:12

ProjectFrank


People also ask

How do you split text in new line?

To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings.

How do I create an array in Shopify?

To create the product array we start with the capture tag so that we can set the new product_list variable equal to all our product information. When capturing the values we separate each value associated with a single product using a | and each set of product values with ::.

How do you split a new line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.


1 Answers

Try this:

{% assign paragraphs = settings.intro | newline_to_br | split: '<br />' %}
{% for paragraph in paragraphs %}<p>{{ paragraph }}</p>{% endfor %}
like image 93
Josh Brown Avatar answered Sep 24 '22 03:09

Josh Brown