newline_to_br¶
The newline_to_br filter is designed to replace newline characters (\n and \r\n) in a string with HTML line break tags (<br />).
Functionality
- Strings: Takes a string as input.
- Replacement: Replaces all newline characters (
\nand\r\n) with<br />tags. - Output: Returns a new string with line breaks converted to HTML line breaks.
Syntax
ArgumentsThe newline_to_br filter does not require any arguments.
Code Samples
Example 1: Converting Newlines in a Paragraph
{% assign paragraph = "This is the first line.\nThis is the second line.\r\nThis is the third line." %}
{{ paragraph | newline_to_br }}
Output:
Example 2: Converting Newlines in a List {% assign items = "Item 1\nItem 2\nItem 3" %}
<ul>
{{ items | newline_to_br | split: "<br />" | join: "</li><li>" }}
</ul>
Outliers and Special Cases¶
- Empty Strings: If the input string is empty, the
newline_to_brfilter returns an empty string. - Strings Without Newlines: If the input string does not contain any newline characters, the filter returns the original string unchanged.
- Non-String Input: If the input is not a string, the
newline_to_brfilter might attempt to convert it to a string or return an error, depending on how Experience Builder handles type conversions.
Key Points¶
- The
newline_to_brfilter is essential for rendering line breaks correctly in HTML output. - It ensures that newlines in your text data are translated into visual line breaks when displayed on a web page.
- You can use this filter in combination with other string filters like
splitandjointo format text blocks, lists, or other structures that rely on line breaks.