How to Convert Newline to Break in Liquid in Shopify

The Liquid can be easily understood as a flexible language. It is useful for plenty of different environments. You would know about some of the popular versions of Liquid such as Shopify Liquid and Jekyll Liquid. In other words, Liquid has been created for use in the Shopify store as well as Jekyll websites.

While you are writing your script, you might want to break your Liquid so that the script will be in the right format and easy to look. In addition, when it is displayed, it will be shown as what you expect. To do this, you will use the tag <br> which is an HTML line break instead of using the newline \n tag.

To be more detailed, the
tag is used to insert a line break. It is usually placed inside a paragraph to break the text down the line, or between two elements to create a space between them.

Below are some simple steps to Convert Newline to Break in Liquid.

Convert Newline to Break in Liquid

Input{#input}

You will use the newline_to_br filter to enter in the input. This is to convert all the newlines to break in Liquid.

{% capture string_with_newlines %}
Hello
there
{% endcapture %}

{{ string_with_newlines | newline_to_br }}

Output{#output}

This output will use the <br> tag mostly. The <br> tag is a single tag, so it doesn’t have a closing tag. In the XHTML, to use a <br> tag, we must add a slash to the tag <br />. The code will look like this:

<br />
Hello<br />
there<br />

Summary

This tutorial has just taken you through how to Convert Newline to Break in Liquid. The filter newline_to_break and the <br> tag used in HTML or the <br /> tag used in XHTML are absolutely helpful for you to make your script has a nice and clean look. Hope you find this post beneficial and feel comfortable to follow. I’m so excited to introduce plenty of smart filters to you. If you are interested in Liquid Item/Items: Automatically Display Singular or Plural of String Based on Number, click the link to visit the writing about it.