How the `linebreaks` and `linebreaksbr` filters convert newline characters in multi-line text to HTML's `

`or`
` tag?

Calendar 👁️ 67

When managing content in Anqi CMS, we often encounter a situation: when multi-line text is entered in the background editing box, it is displayed as a single line on the front-end page, or the newline characters are displayed as text directly. This is because browsers default to ignoring single newline characters when rendering HTML (\nIf you want the content to be displayed like it is in the editing box, you need to use the powerful template filter provided by AnQiCMS, especiallylinebreaksandlinebreaksbr.

These filters are practical tools in the AnQiCMS template engine, their function is to convert line breaks in text content into HTML tags that browsers can recognize and render, thereby ensuring that your multi-line text is displayed as expected on the website.

linebreaksbrFilter: The preferred choice for simple line breaks

linebreaksbrThe filter, as the name implies, will replace all line breaks in the text (\n) directly with HTML's<br/>Label. This is very useful for those who need simple line breaks instead of paragraph breaks, such as:

  • Address informationWhen you need to display detailed address on the contact page, you may want each address element to be displayed independently, rather than being cramped together.
  • Product Features ListIf the product description contains multiple short features, it is desired that each feature occupy a separate line,linebreaksbrit can be put to use.
  • poetry or lyricsThese contents usually require strict line-by-line display.

Usage example:

Assuming your backend text content is:

公司名称:AnQiCMS
联系电话:123-4567-8900
公司地址:某某市某某区某某街道123号

In the template, you can use it like thislinebreaksbr:

<div class="contact-info">
    <h3>联系方式</h3>
    <p>{{ system.contact_details|linebreaksbr|safe }}</p>
</div>

After rendering, the browser will display as:

<div class="contact-info">
    <h3>联系方式</h3>
    <p>公司名称:AnQiCMS<br/>联系电话:123-4567-8900<br/>公司地址:某某市某某区某某街道123号</p>
</div>

In this way, each piece of information will occupy a separate line on the page.

It is noteworthy that when using this filter, it is usually necessary to add|safeFilter. This is because AnQiCMS (and many other template engines) for security reasons, by default, will escape the output HTML content, to<to&lt;etc.|safeTell the template engine that this part of the content is safe, no escaping is needed, and it can be parsed directly as HTML. If it is missing|safeyour<br/>Tags may be displayed in plain text form rather than actual line breaks.

linebreaksFilter: A Tool for Semantically Structuring Paragraphs.

withlinebreaksbrCompared tolinebreaksThe filter provides a more semantic processing method, it focuses more on converting multi-line text into structured paragraphs. Specifically:

  • It will convert a single newline character to<br/>tags, likelinebreaksbr.
  • But more importantly, it will convert two or more consecutive newline characters (usually represented as the start of a new paragraph in a text editor) to HTML.<p>and</p>Paragraph wrapped in tags.

This processing method makeslinebreaksThe filter is very suitable for processing long articles, blog posts, or any text that requires clear paragraph structure. It can better preserve the original structure of your content and make it more visually readable, and it is also more friendly to search engines because it uses semanticized<p>.

Usage example:

Assuming your backend article content is:

这是文章的第一段内容。
它包含一些重要的信息,并且在这里结束。

这是文章的第二段内容。
它与第一段通过空行分隔,理应是独立的段落。

In the template, you can use it like thislinebreaks:

<div class="article-content">
    <h2>{{ article.title }}</h2>
    <div class="main-body">
        {{ article.full_content|linebreaks|safe }}
    </div>
</div>

After rendering, the browser will display as:

<div class="article-content">
    <h2>文章标题</h2>
    <div class="main-body">
        <p>这是文章的第一段内容。<br/>它包含一些重要的信息,并且在这里结束。</p>
        <p>这是文章的第二段内容。<br/>它与第一段通过空行分隔,理应是独立的段落。</p>
    </div>
</div>

Here, the content is correctly divided into two<p>paragraphs, and the line breaks within the paragraph are<br/>represented. Similarly,|safethe filter is also indispensable here.

How to chooselinebreaksandlinebreaksbr?

The choice of filter depends mainly on the semantics and expected display effect of your content:

  • If you need is a simple inline line break, not involving explicit paragraph concepts, such as addresses, short sentence lists, or product features, thenlinebreaksbrit is a lighter and more direct choice.
  • If you are processing a long article, news content, or any other long text that requires clear paragraph structure and you want to preserve the original intent of the paragraphs, thenlinebreaksProvide more semantically meaningful HTML output, which helps improve the readability and SEO friendliness of the content.

They are all practical tools in AnQiCMS template development, enhancing user experience and content presentation quality.By flexibly using these two filters, you can easily solve the problem of displaying multiline text on web pages, making your content more beautiful and readable on AnQiCMS-driven websites.

Further reading:linenumbersFilter

In addition to handling line breaks, AnQiCMS also provideslinenumbersA filter that can number each line of multi-line text, starting from 1, like "1. ".For example, if you need to display content with line numbers in a code block or some specific text area,{{ code_block|linenumbers|safe }}it can be put to use.


Frequently Asked Questions (FAQ)

  1. Q: Why did I uselinebreaksorlinebreaksbrFilter, but the page still displays plain text<br/>or<p>Tag, rather than actual line breaks or paragraphs?

    • A:This is usually because you forgot to add at the end of the filter chain|safeThe filter. AnQiCMS to prevent XSS attacks, the default will escape all HTML tags to the corresponding entity characters (such as<to&lt;)|safeThe filter tells the template engine that this part of the content is safe HTML that can be rendered as is without escaping. So, make sure your template code is{{ variable|linebreaksbr|safe }}or{{ variable|linebreaks|safe }}.
  2. Q: Do these filters automatically handle Markdown syntax?For example, is the text input to my backend in Markdown format directly convertible to HTML?

    • A:No.linebreaksandlinebreaksbrThe filter focuses solely on converting native line breaks(\nConvert to HTML's<br/>or<p>tags. They do not parse or convert Markdown syntax. If your content is in Markdown format, you

Related articles

How to get the length of a string, array, or key-value pair in the `length` and `length_is` filters of the Anqi CMS template?

In Anqi CMS template development, it is often necessary to dynamically adjust the display of the page according to the length of the data content.Whether it is to truncate text, judge whether the list is empty, or perform simple content verification, understanding how to obtain the length of strings, arrays, or key-value pairs is the foundation of these functions.AnQi CMS provides the `length` and `length_is` filters, which can help developers flexibly handle these requirements.

2025-11-08

How does the `join` filter concatenate elements of an array into a single string using a specified delimiter?

In Anqi CMS template design, we often encounter the need to integrate a series of data items into a coherent text.For example, we need to display multiple tags (Tag) in one place, or combine a set of custom parameter values obtained from the database.At this point, the `join` filter comes into play, which can efficiently concatenate the elements of an array into a string with the specified separator.### Understand `join`

2025-11-08

How do the `integer` and `float` filters convert strings to integers or floating-point numbers and handle conversion failure situations?

In web template development, flexible handling of data types is the key to ensuring correct content display.We often encounter situations where we need to convert string data obtained from databases or external interfaces into numbers for calculation or formatting display.AnQiCMS (AnQiCMS) fully considers this requirement, providing two built-in filters `integer` and `float` to help users easily convert strings to integers or floating-point numbers, and intelligently handle conversion failure cases, thereby enhancing the robustness of the template.

2025-11-08

How does the `index` filter find the first occurrence of a keyword in a string or array?

In AnQi CMS template development, we often need to fine-tune the display of content, which includes flexible handling of string and array content.Understand and make good use of the various template filters provided by Anqi CMS, which can greatly enhance our ability to build dynamic and intelligent websites.Today, let's delve into a very practical filter——`index`, which can help us accurately locate the first occurrence of a keyword in a string or array.### `index` filter: A powerful tool for precise keyword positioning Imagine that

2025-11-08

How does the `linenumbers` filter add line number markers to each line of multiline text?

In website content display, sometimes we need to add line numbers to specific multi-line text content, such as code examples, step-by-step tutorials, or log information, to enhance readability and facilitate reference.AnQiCMS provides a simple and practical template filter `linenumbers`, which can help us easily achieve this function. ### The `linenumbers` filter's purpose The `linenumbers` filter is specifically used to automatically add line number markers to each line of multi-line text.It will start from the number 1

2025-11-08

How to define a string array variable directly in the template using the `list` filter?

AnQiCMS with its flexible and powerful template engine, provides great convenience for content display.When using templates for front-end development, we often need to handle various data, among which array variables are a common and practical data structure.Most of the time, we may need to define some fixed or temporary string arrays directly in the template, rather than passing them through backend code each time.Fortunately, AnQiCMS provides a very convenient `list` filter, making this operation extremely simple.### Core Function Analysis: `list`

2025-11-08

How does the `phone2numeric` filter convert letters on a mobile phone's numeric keypad to the corresponding numbers?

In AnQiCMS template development, we often need to handle various data, and sometimes we may encounter some special situations with phone number input and display.For example, some phone numbers include letters (also known as "pretty numbers" or "vanity numbers", such as 1-800-FLOWERS) for ease of memorization or brand promotion.However, when dialing in practice, these letters need to be converted to the numbers on the corresponding number keypad.AnQiCMS provides a very practical built-in filter——`phone2numeric`, which helps us easily complete this conversion

2025-11-08

How to use the `random` filter in Anqie CMS template to randomly return a character or value from a string or array?

In AnQi CMS template development, sometimes we hope to add a touch of dynamism and surprise to the website content, so that visitors can see different elements each time they refresh the page.This is a very practical tool, the `random` filter.It can help us randomly select one from a set of predefined data to display, whether it is randomly selecting characters from a string or randomly selecting a value from an array (or list), it can be easily achieved.

2025-11-08