In AnQiCMS template development, we often need to display some user input plain text content, such as product descriptions, article summaries, etc., in the web page while preserving the original line break format. In order to achieve this purpose, AnQiCMS has built-in a series of practical filters, includinglinebreaksbrIs a tool specifically designed to handle newline characters. However, many users may have a question about this filter: does it really only responsible for simply converting newlines to HTML?<br/>What is the tag? Today, let's delve deeply into this issue.

linebreaksbrThe core function: simple direct line break conversion

As the name implies,linebreaksbrThe core role of the filter is to convert all line breaks in plain text content to\n) directly replace with HTML's<br/>tag. It focuses on the "inline break" scenario, ensuring that each line of text between<br/>A tag implements visual line breaks without introducing additional block-level elements (such as<p>Label).This conversion method is very suitable for those who want the text to be compact and only broken at the necessary places.For example, if you have a brief address information, or a piece of text that requires precise control of line spacing,linebreaksbrit can be put to use.

Consider the following plain text entered in the AnQiCMS content field:

第一行文本
第二行文本
第三行文本

When you use this field in the templatelinebreaksbrWhen using filters:

{{ archive.Description|linebreaksbr }}

The final HTML rendered in the browser will be:

第一行文本<br/>
第二行文本<br/>
第三行文本

This clearly indicates,linebreaksbrThe responsibility is indeed to accurately convert line breaks to<br/>Tags, do not add any other HTML structure.

withlinebreaksThe difference between filters: structured vs. concise choice.

AnQiCMS has another filter with similar functionality but different behavior——linebreaks. Understandlinebreaksin a way that can better help us distinguish it fromlinebreaksbr.

linebreaksThe filter takes a more "structured" approach when handling text line breaks. It will take the text andA single newlineto<br/>labels, andlinebreaksbrsimilar. But when encounteringtwo or more consecutive line breaksthen,linebreaksIt will treat it as a new paragraph and use<p>The tag wraps the text before and after. This means thatlinebreaksThe text will be paragraphed.

Continue with the above text as an example, if we modify it and uselinebreaksFilter:

第一段内容

第二段内容
这是第二段的第二行

UselinebreaksFilter:

{{ archive.Description|linebreaks }}

The rendering result will be as follows:

<p>第一段内容</p>
<p>第二段内容<br/>
这是第二段的第二行</p>

It is obvious,linebreaksIt is more suitable for handling texts that contain natural paragraphs, and it can generate paragraph structures that are more in line with HTML semantics. AndlinebreaksbrIt focuses more on simple inline breaks, without introducing paragraph concepts. Therefore, when you need to decide which filter to use, the key is to ensure that the final HTML output is a concise line break (linebreaksbr),or a more semantically structured paragraph structure(linebreaks)

Key considerations when using filters: safety and rendering

whether it islinebreaksbrOrlinebreaksThey are mainly used to convert line breaks in plain text to HTML tags.This means that if your source text may contain other HTML code (such as content entered through a rich text editor), then these filters are not very applicable because they try to handle all line breaks, which may interfere with existing HTML structures.

In addition, since both of these filters generate HTML content, to ensure that the browser parses it correctly and to avoid potential security issues (such as cross-site scripting attacks XSS, although AnQiCMS has its own security mechanisms), it is usually necessary to use them in conjunction with templates when outputting.|safea filter. For example:

{{ archive.Description|linebreaksbr|safe }}

|safeThe filter tells AnQiCMS's template engine that this content is "safe" HTML and does not need to be escaped again. If not|safe,<br/>these tags may be escaped to&lt;br/&gt;This cannot be displayed correctly due to line breaks.

Summary

Return to the original question, in AnQiCMSlinebreaksbrThe filter is indeed a specialized function.