In AnQiCMS template development, we often need to display some plain text content entered by users, such as product descriptions, article summaries, etc., on the web page in a format that preserves the original line breaks. To achieve this, AnQiCMS has built-in a series of practical filters, wherelinebreaksbrIt is a tool specifically designed to handle newline characters. However, for this filter, many users may have a question: does it really just responsible for converting newlines to HTML's<br/>Are we talking about tags? Today, let's delve into this issue in depth.
linebreaksbrThe core feature: simple and direct line break conversion
As the name suggests,linebreaksbrThe core function of the filter is to convert all line breaks in plain text content to\n)directly replace it with HTML's<br/>tag. It focuses on the 'inline wrapping' scenario, ensuring that each line of text can be wrapped through<br/>Tags implement a visual break effect without introducing additional block-level elements (such as<p>Tag").This conversion method is very suitable for those who want the text to be compact and only break at the necessary places.linebreaksbrit can be very useful.
Consider the following plain text entered in the AnQiCMS content field:
第一行文本
第二行文本
第三行文本
When you use this field in the template,linebreaksbrthe filter:
{{ archive.Description|linebreaksbr }}
the final HTML rendered in the browser will be:
第一行文本<br/>
第二行文本<br/>
第三行文本
This clearly indicates that,linebreaksbrthe responsibility is indeed to accurately convert line breaks into<br/>Tags, do not add any other HTML structure.
WithlinebreaksDifference between filters: choice of structure and conciseness
AnQiCMS also has a filter with a similar function but different behavior -linebreaks. Understandinglinebreaksthe working mode can better help us distinguish it fromlinebreaksbr.
linebreaksThe filter takes a more "structured" approach when processing text line breaks. It will divide the text intoSingle line breakConverted to<br/>tags, andlinebreaksbrsimilar. However, when encounteringtwo or more consecutive line breakswhenlinebreaksIt will consider it as a new paragraph and use<p>tags to wrap the text before and after. This means,linebreaksthe text will be paragraphed.
Continue with the above text as an example, if we make a slight modification and uselinebreaksFilter:
第一段内容
第二段内容
这是第二段的第二行
UselinebreaksFilter:
{{ archive.Description|linebreaks }}
The rendering result will be like this:
<p>第一段内容</p>
<p>第二段内容<br/>
这是第二段的第二行</p>
It is obvious,linebreaksMore suitable for handling texts that contain paragraphs, it can generate paragraph structures that are more in line with HTML semantics.linebreaksbrThen it is more focused on simple inline breaks, without introducing paragraph concepts. Therefore, when you need to decide which filter to use, the key is whether you want the final HTML output to be concise line breaks (linebreaksbr),or more semantically structured paragraphslinebreaks).
Important considerations when using filters: security and rendering
WhetherlinebreaksbrOrlinebreaksThey are mainly used to convert newline characters in plain text to HTML tags.This means that if your source text may contain other HTML code (such as content input through a rich text editor), these filters are not very applicable because they will try to process all line breaks, which may interfere with existing HTML structure.
In addition, since both of these filters generate HTML content, to ensure that the browser correctly parses it and to avoid potential security issues (such as cross-site scripting XSS, despite AnQiCMS having its own security mechanisms), it is usually necessary to use them in conjunction with outputting to the template.|safefilter. 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 added|safe,<br/>tags may be escaped to<br/>Therefore, the line break effect cannot be displayed correctly.
Summary
Return to the original question, in AnQiCMSlinebreaksbrThe filter is indeed a function with a single focus