In the template design of AnQi CMS, we often use various filters to conveniently process and display content. Wherein,linebreaksFilter is a very practical tool, it can intelligently convert line breaks in plain text to paragraphs in HTML<p>Tags and line breaks<br/>Labels, thus presenting more readable layouts on web pages. However, some users may encounter such situations: even brief phrases or content that should not be considered as independent paragraphs may be unexpectedly enclosed in<p>The label may lead to page style chaos or the generation of HTML structures that do not conform to semantics. Understanding how to avoid this issue is crucial for building high-quality websites.
UnderstandinglinebreaksThe working principle of the filter
linebreaksThe filter is designed to automatically convert the original text entered by the user (usually entered in a text editor, separated by line breaks to separate paragraphs) into a paragraph structure that conforms to HTML standards. Its core logic is:
- Continuous two newline characters(which is a blank line) is interpreted as a paragraph separator, and the text content between them will be
<p>each enclosed by tags. - Single line breakis converted to
<br/>tag, indicating a line break within the current paragraph.
The issue lies here: If your template variable{{ some_text }}contains a phrase, and this phrase is exactly on a line by itself (or it is preceded or followed by a newline character),linebreaksThe filter will treat it as a complete “paragraph”, and<p>Label it.
For example,{{ archive.Description }}The value of the variable is:
这是一个简短的摘要。
After{{ archive.Description|linebreaks }}After processing, the output is likely to be:
<p>这是一个简短的摘要。</p>
This is for those who originally only wanted to be plain text (such as part of a list item, or an elementtitleThe content of the attribute value) is obviously not an ideal result. It may cause excessive vertical spacing, incorrect font styles, or even destroy the overall layout.
linebreaksWithlinebreaksbr: Clarify your needs.
In the AnQi CMS, besides other options,linebreaksThere is also a filter with similar functionality but different behavior:linebreaksbrUnderstanding the difference between the two is to avoid unexpected outcomes:<p>The first key step in labeling tags:
linebreaks:适用于将多行文本转换为 HTML 段落(English)<p>) and line breaks(<br/>English scenarios.It focuses more on simulating the paragraph division in traditional text layout.English translation: Typically used for long text content entered by users, such as article content, comment content, product descriptions, etc., which need to be displayed as structured paragraphs on the page.linebreaksbr:仅将文本(English)