When using AnQiCMS for content creation and template design, how to precisely control the display format of content is a common issue faced by website operators.Especially how the return (newline character) entered by the user in the text editor is presented on the frontend of the page, this involves the working mechanism of the template filter.Today, let's delve deep intolinebreaksbrThe filter answers a common question: it converts consecutive multiple line breaks into a single<br/>or multiple<br/>?

linebreaksbrThe principle of the filter.

The answer is:linebreaksbrThe filter will process text inEvery oneA newline character (such as pressing the Enter key in a text editor generates\n) is converted into an independent<br/>tag. It does not intelligently merge consecutive multiple newline characters.

This means, if you enter two line breaks while editing content, for example:

第一行内容
(这里输入了一次回车)
第二行内容
(这里输入了两次回车)


第三行内容

afterlinebreaksbrAfter the filter is processed, the following HTML structure will be generated on the page:

第一行内容<br/>
第二行内容<br/><br/>
第三行内容

You can see that the two consecutive newline characters in the original text are accurately converted into two consecutive<br/>This behavior is similar to that in PHPnl2brThe function is very similar, its main purpose is to directly map all line breaks in plain text to HTML line break tags.

linebreaksbrwithlinebreaksDifference

AnQi CMS provides two filters for handling newline characters:linebreaksbrandlinebreaksThey are all aimed at converting line breaks in text to HTML structures, but the methods are slightly different and are suitable for different scenarios.

  • linebreaksbr:As described above, it directly converts each original newline character\nto<br/>. It does not add any content at the beginning or end of the content<p>Labels, nor will it specially handle blank lines (i.e., multiple consecutive newline characters).This makes it very suitable for scenarios that require strict inline line breaks, such as address information, poetry, or code snippets, where you want each return key to be accurately reflected in the final display effect.

  • linebreaks:This filter is more powerful in terms of functionality, it will try to parse plain text content into HTML paragraphs. Specifically, it will treat the content between every two newline characters (i.e., an empty line) as a paragraph, and use<p>Tags wrap around. A single newline character is converted to<br/>Tags. This approach is more in line with the semantics of traditional article paragraphs, for example:

    这是一个段落。
    (一个回车)
    这是段落内的强制换行。
    
    
    (两个回车,形成一个空行,开始新段落)
    这是另一个新的段落。
    

    afterlinebreaksAfter filtering, it may generate HTML like this:

    <p>这是一个段落。<br/>这是段落内的强制换行。</p>
    <p>这是另一个新的段落。</p>
    

    obviously,linebreaksIt is more suitable for processing content like article text that requires automatic segmentation.

Actual application in AnQiCMS template

No matter which filter is used, in Anqie CMS templates, they are all represented by a pipe character|The form should be applied to the variable. Moreover, since these filters generate HTML tags, to prevent the browser from escaping the characters within the<and>such as escaping the characters (e.g., converting<br/>becomes&lt;br/&gt;), usually needs to be accompanied by|safeto use the filters together.

Example:If you have a variable for the content of an articlearchive.Content, hope that the line breaks can be presented in the form of HTML tags, you can use them like this:

{# 使用 linebreaksbr,适用于需要精确控制每一行换行的场景 #}
<div>
    {{ archive.Content | linebreaksbr | safe }}
</div>

{# 使用 linebreaks,适用于将文本内容自动分段的场景 #}
<div>
    {{ archive.Content | linebreaks | safe }}
</div>

By selecting the appropriate filter, you can flexibly control the layout and display effects of content in Anqi CMS, thereby better meeting the operational needs and user experience of the website.


Frequently Asked Questions (FAQ)

  1. Q: When should I choose?linebreaksbrWhen to choose,linebreaksWhat?A: When you need to convert each newline in the text precisely into a<br/>tag, and do not want the content to be<p>wrapped by the tag, choose.linebreaksbrFor example, when displaying address information, poetry, or concise descriptions. And when you want to automatically parse the plain text input by users (including blank lines) into paragraphs that comply with HTML semantics (that is, using<p>when enclosed in tagslinebreaksis a better choice, it is more suitable for handling articles, blog posts, etc.

  2. Q: If I do not use any newline filter, how will AnQi CMS handle newline characters in the text?A: If you directly output a text variable containing a newline in the template without usinglinebreaksbrorlinebreaksFilter, the browser will process it according to the standard behavior of HTML. This means that the browser will ignore a single newline character, treating it as a space; only consecutive multiple newline characters may be rendered by the browser as one or more spaces, but will not generate<br/>or<p>The label causes the content to be挤在一行when displayed on the page.

  3. Q:linebreaksbrThe filter will handle the existing text in the text.<br/>Am I supposed to use the label?A: No.linebreaksbrThe filter only recognizes and converts the newline characters in the original text (such as \n/\r\nIt will not parse or modify any existing HTML tags, including<br/>Labels. Therefore, if your content already contains HTML tags, make sure that these tags are correct, and they still need to be combined after using the filter|safeTo prevent them from being escaped.