How can I split a string by newline in Shopify?

Try this:

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

@josh-browns answer got me pretty much there and might be enough for most instances. However, I had some blank paragraphs coming through from user generated double lines.

Denilson Sá Maia comment of strip_newlines did not help with my markdown processor so I checked if each line was empty manually before outputting the paragraph.

{% for paragraph in paragraphs %}
    {% assign paragraph-length = paragraph | number_of_words %}
    {% if paragraph-length > 0 %}
        <p>{{ paragraph }}</p>
        IMAGE HERE
    {% endif %}
{% endfor %}

This doesn't solve the blanks in the array, for that I iterated over the array to create a new one, with only valid content. It would be really nice if there was a filter that worked on arrays, not just hashes.

{% assign all-paragraphs = content  | strip | newline_to_br | strip_newlines | split: "<br />"  %}
{% assign paragraphs = site.emptyArray  %}
{% for paragraph in all-paragraphs %}
    {% if paragraph and paragraph != "" and paragraph != site.emptyArray %}
         {% assign paragraphs = paragraphs | push: paragraph %}
    {% endif %}
{% endfor %}

Tags:

Liquid

Shopify