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?
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.
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 ::.
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.
Try this:
{% assign paragraphs = settings.intro | newline_to_br | split: '<br />' %}
{% for paragraph in paragraphs %}<p>{{ paragraph }}</p>{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With