Skip to content

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 (\n and \r\n) with <br /> tags.
  • Output: Returns a new string with line breaks converted to HTML line breaks.

Syntax

    {{ input_string | newline_to_br }}
Arguments

The 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:

    This is the first line.<br />This is the second line.<br />This is the third line.
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>
Output:

    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>

Outliers and Special Cases

  • Empty Strings: If the input string is empty, the newline_to_br filter 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_br filter 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_br filter 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 split and join to format text blocks, lists, or other structures that rely on line breaks.