How to precisely control the display format of content when creating content and designing templates in AnQiCMS is a common issue faced by website operators.Especially how the return (newline character) entered by the user in the text editor is presented in the frontend of the page, which involves the working mechanism of the template filter.linebreaksbrFilter, and answer a common question: it will convert consecutive multiple newline characters to a single<br/>or multiple<br/>?

linebreaksbrThe working principle of the filter

The answer is:linebreaksbrThe filter will convert text inEvery oneLine break (such as pressing the Enter key in a text editor produces)\nAll of these are converted into separate)<br/>tags. It does not intelligently merge consecutive multiple line breaks.

This means that 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/>
第三行内容

It can be seen that the two consecutive newline characters in the original text are accurately converted into two consecutive newlines<br/>This behavior is consistent with PHP'snl2brThe function is very similar, its main purpose is to directly map all newline characters in plain text to HTML newline tags.

linebreaksbrWithlinebreaksdifferences

The AutoCMS provides two filters for handling newline characters:linebreaksbrandlinebreaks. They are all aimed at converting line breaks in text to HTML structure, but the processing methods are slightly different and are suitable for different scenarios.

  • linebreaksbr:As described above, it directly converts each original newline character\nConverted to<br/>. It does not add any content at the beginning or end<p>Labels, and will not treat blank lines (i.e., consecutive multiple newline characters) specially.This makes it very suitable for scenarios where strict inline line breaks need to be maintained, such as address information, poetry, or code snippets, where you want each press of Enter to be precisely 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., one blank line) as a paragraph, and<p>Wrap tags. A single newline character is converted to<br/>tags. This processing method is closer to the semantics of traditional article paragraphs, for example:

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

    AfterlinebreaksFiltering may generate HTML like this:

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

    Clearly,linebreaksmore suitable for processing content like article body that requires automatic segmentation.

The actual application in AnQiCMS template

No matter which filter is used, in the template of Anqi CMS, they are all represented by the pipe symbol|The form applied to the variable. In addition, since these filters generate HTML tags, to prevent the browser from escaping them twice (for example, escaping the characters in the tags),<and>such as escaping the characters in the tags.<br/>becomes&lt;br/&gt;),usually requires to be paired with|safeFilter used together.

Example:If you have a variable for article contentarchive.Content,you 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 the safe CMS, thereby better meeting the operational needs and user experience of the website.


Common Questions (FAQ)

  1. Q: When should I chooselinebreaksbrwhen to chooselinebreaks?A: When you need to convert every newline in the text exactly to a<br/>tag, and do not want the content to be<p>Label wrapping, selectlinebreaksbr,for example, when displaying address information, poetry, or some concise and forceful descriptions. And when you want to automatically parse the plain text content (including blank lines) entered by the user into paragraphs that comply with HTML semantics, that is,<p>Label wrapping)when,linebreaksis a better choice, it is more suitable for handling articles, blog posts, and other content.

  2. Q: How will AnQi CMS handle newline characters in text if I do not use any newline filter?A: If you directly output a text variable containing a newline character in the template without usinglinebreaksbrorlinebreaksFilter, the browser will handle it according to the standard behavior of HTML. This means that the browser will ignore single line breaks, treating them as spaces; only consecutive multiple line breaks may be rendered by the browser as one or more spaces, but will not generate<br/>or<p>Tags, causing the content to be displayed on one line.

  3. Q:linebreaksbrThe filter will process the text that already exists.<br/>the tags?A: No.linebreaksbrThe filter only recognizes and converts the newline characters in the original text (such as\n/\r\n)。It will not parse or modify the HTML tags that already exist in the text, including<br/>Label. Therefore, if your content already contains HTML tags, make sure these tags are correct, and they still need to be combined after using the filter.|safeTo prevent them from being escaped.