In AnQiCMS template development, dealing with multi-line text content is a common requirement. To better display user input or text stored in the database with line breaks, the system provideslinebreaksandlinebreaksbrThese convenient filters.However, some users may be wondering whether these filters will cause character encoding issues or affect the normal display effect when handling multi-line Chinese text.
Let's make it clear first,In the Anqi CMSlinebreaksThe filter does not directly affect character encoding.Its core function is to convert the standard newline character in the text into HTML paragraph tags (\n)<p>)and newline tags<br/>Thus, formatted multi-line text effect is presented on the web.Anqi CMS is a system developed based on the Go language, which uses UTF-8 encoding for its template files, as also explicitly stated in the documentation.Go language itself has good support for UTF-8 character set, which means that whether it is English, Chinese or other languages, as long as the system environment and the file itself use UTF-8 encoding correctly, the filter will not change the original character encoding format when processing text content, and will not cause garbled characters.
So,linebreaksWhat impact does the filter have when displaying multi-line Chinese text?This is exactly what its design初衷 and value lie in.
- Consecutive single newline characters
\n)Converted to HTML<br/>Label. - Consecutive two or more newline characters (equivalent to paragraph separators)Converted to a pair
<p>...</p>Label the paragraph content and use single-line breaks within the paragraph.<br/>Label.
For example, a Chinese text segment:
这是第一行中文。
这是第二行中文。
这是新段落的中文。
AfterlinebreaksAfter filtering, it may be displayed as HTML:
<p>这是第一行中文。<br/>这是第二行中文。</p><p>这是新段落的中文。</p>
The browser will parse this HTML and create a new paragraph based on<p>a tag and add it to the document,<br/>Forces a line break to achieve a display effect similar to the original text formatting. This conversion is also applicable to Chinese text, because Chinese characters are just ordinary character sequences in UTF-8 encoding, with no essential difference from English or other languages' characters.linebreaksThe filter only recognizes common newline characters and does not 'understand' the language of the text.
It should be noted that,linebreaksThe filter generates HTML tags, in order to ensure that these tags are rendered correctly on the page instead of being displayed as text,you must use when outputting variables|safeFilter.Just like the Django template engine, the template system of Anqin CMS defaults to escaping all output content as HTML entities to prevent cross-site scripting (XSS) attacks. If you forget to add|safeThe browser will treat it as plain text and display it directly, causing an "unnatural" appearance.<p>and<br/>This will result in an "unnatural" display, as the browser will treat it as plain text and display it directly.
In summary, the Anqi CMS'slinebreaksThe filter works the same when processing multi-line Chinese text as it does with text in other languages.It efficiently and reliably converts plain text line breaks into web-recognizable HTML structure without introducing character encoding issues.|safeYou will be able to smoothly display formatted multi-line Chinese text content.
Frequently Asked Questions (FAQ)
Why does my multi-line Chinese text go
linebreaksafter processing, but the browser shows<p>and<br/>such tags instead of the expected line break effect?This is usually because you forget to use|safeFilter. The template system of Anqi CMS defaults to escaping all output content to HTML entities to prevent potential security issues. WhenlinebreaksThe filter has generated<p>and<br/>these HTML tags are not used|safeMarked as “safe content”, the template system will escape them as plain text (for example<becomes<), causing the tags to be displayed directly on the page. The solution is to uselinebreaksAfter the filter, then add|safefor example{{ archive.Content|linebreaks|safe }}.I used
linebreaksThe filter, but Chinese text still appears as garbled, what is the reason?linebreaksThe filter itself does not cause character encoding issues. If garbled characters appear, it is usually due to one or more of the following problems:- The encoding of the template file is incorrect:Your template file may not be saved as UTF-8 encoding. Please check and make sure all template files are in UTF-8 encoding.
- The HTML page does not declare UTF-8:Your HTML page
<head>Part may not have declared character set correctly, i.e., missing<meta charset="UTF-8">. - Database or data source encoding issue:When storing Chinese content in a database field or during data import, UTF-8 encoding may not have been used.
linebreaksThe filter works well under UTF-8 environment, so the garbled code problem needs to be investigated from the source.
linebreaksandlinebreaksbrWhat are the actual differences when handling multi-line Chinese text? How should I choose?The main difference between them lies in how they handle paragraphs.linebreaks:It will convert the single-line newline characters in the text to<br/>and wrap text blocks separated by two or more consecutive newline characters in<p>...</p>Tags within the content, forming semantic paragraphs. This is more in line with HTML standards and is suitable for displaying segmented text such as article content, product descriptions, etc., which is also more SEO-friendly.linebreaksbr:Then simply replace all single-line newline characters with<br/>tags without creating<p>Tag.This approach is more 'original', typically used when only a simple implementation of text wrapping is needed, without emphasizing semantic paragraphs, for example, address information or short list items.linebreaks; if you just need simple inline line breaks, you can uselinebreaksbr. In any case, it needs to be used with|safeUse.