How to automatically convert newline characters in multi-line text in AnQiCMS templates to HTML's `
` tag?

Calendar 👁️ 76

How to make the newline characters of multi-line text automatically display as HTML in the AnQiCMS template<br/>Tag?

In website content operation, we often encounter situations where it is necessary to display multi-line text, such as article summaries, product descriptions, or user comments, etc.This text content is typically created by pressing the Enter key during background editing.\nThe text is output directly to the HTML page, and the browser will not interpret it as a visual line break.On the contrary, HTML defaults to collapsing multiple spaces and line breaks into a single space, causing the originally line-formatted content to display as a long string of text.

To solve this problem, we need to convert the invisible newline characters in the text\nConvert to a line break tag that browsers can recognize<br/>. AnQiCMS's powerful template engine provides a simple and efficient 'filter' function that helps us easily achieve this transformation.

Understand the text processing filter in AnQiCMS templates

AnQiCMS's template engine borrows from Django template syntax, providing a variety of built-in filters to handle and format output content. For the need of line breaks in multi-line text, there are two very practical filters:linebreaksbrandlinebreaks.

  1. linebreaksbrFilterThis filter is as intuitive as its name, it will replace each newline character in the text\ndirectly<br/>Label. This is very suitable for those who only need to display each line of text line by line, without needing additional paragraph formatting. Its effect is similar to the function in PHP.nl2brFunction.

  2. linebreaksFilter linebreaksThe filter provides a more intelligent paragraph processing method. It will convert a single newline\nto<br/>However, if it encounters two consecutive newlines\n\n(usually indicating an empty line), it will convert it to<p>...</p>Paragraph tag, and use<br/>To handle single-line breaks within paragraphs. This is more suitable for scenarios where you need to format multi-line text content into HTML paragraphs.

How to apply these filters in AnQiCMS template?

Using these filters is very simple, just add them after the variable you need to process|过滤器名称Just do it.

Assuming we have a document description field obtained from the backgroundarchive.DescriptionIt includes multiple lines of text:

Example of background input content:

这是第一行文本。
这是第二行文本。

这是第三行文本,前面有一个空行。
第四行。

1. UselinebreaksbrThe filter implementation is simple line breaking

If you want the content to be displayed simply line by line, one line per line<br/>you can use the taglinebreaksbr:

<div class="description-content">
    {{ archive.Description|linebreaksbr|safe }}
</div>

The output HTML will be:

<div class="description-content">
    这是第一行文本。<br/>
    这是第二行文本。<br/>
    <br/>
    这是第三行文本,前面有一个空行。<br/>
    第四行。
</div>

2. UselinebreaksThe filter implements paragraph formatting

If you want content to be organized in paragraph form, text separated by blank lines becomes independent<p>Tags, and line breaks within paragraphs become<br/>thenlinebreaksFilters are a better choice:

<div class="description-content">
    {{ archive.Description|linebreaks|safe }}
</div>

The output HTML will be:

<div class="description-content">
    <p>这是第一行文本。<br/>
    这是第二行文本。</p>
    <p>这是第三行文本,前面有一个空行。<br/>
    第四行。</p>
</div>

3. Important|safeFilter: Avoid HTML tags from being escaped

While usinglinebreaksbrorlinebreaksAfter the filter, they will generate HTML tags (such as<br/>or<p>)。The AnQiCMS template engine, to prevent cross-site scripting (XSS) attacks, defaults to encoding all output content as HTML entities.<br/>such strings instead of actual line breaks.

To ensure that the generated HTML tags can be correctly parsed by the browser, we need to addlinebreaksbrorlinebreaksa filter after that, then add another one|safefilter.safeThe filter tells the template engine that this content is safe and does not require HTML entity encoding and can be output directly.

The correct usage is always:{{ 变量|过滤器|safe }}.

Error example (do not use)|safe):

{{ archive.Description|linebreaksbr }}

Possible output page content (note)<br/>It will be displayed as plain text):

这是第一行文本。&lt;br/&gt;
这是第二行文本。&lt;br/&gt;

Correct example:

{{ archive.Description|linebreaksbr|safe }}

What you will actually see in the browser:

这是第一行文本。
这是第二行文本。

Summary

Whether it is a simple inline line break or you want to format the text intelligently into an HTML paragraph, AnQiCMS provides thelinebreaksbrandlinebreaksFilters can meet your needs. Remember to add after using these filters to convert plain text to HTML tags.|safeA filter to ensure that the browser can correctly parse and render these tags, thereby making your multi-line text content presentable and clear on the front-end page.


Frequently Asked Questions (FAQ)

1. Why did I uselinebreaksbrFilter, but what is displayed on the page is<br/>text rather than actual line breaks?

This is likely because you forgot to add at the end of the filter chain|safeFilter. The AnQiCMS template engine defaults to encoding all output content as HTML entities to prevent security issues. When you uselinebreaksbrorlinebreaksThe filter has generated<br/>or<p>After the HTML tag, if not|safemarked as 'safe content', these tags will be encoded to&lt;br/&gt;or&lt;p&gt;Then display it on the page in plain text. Make sure your code is{{ 变量|linebreaksbr|safe }}.

2.linebreaksandlinebreaksbrWhat are the differences between filters? How should I choose?

The main difference lies in the way they handle blank lines.

  • linebreaksbr: Convert each individual newline character\ndirectly to<br/>Label. It does not automatically create paragraphs<p>Label. If you just want to display each line of text independently, without any paragraph concept, or if your text is already wrapped in other block-level elements (such as<li>Thenlinebreaksbris the better choice.
  • linebreaksTranslate a single newline character\nto<br/>Tag, but will convert consecutive two newline characters\n\n(Representing an empty line) converts to HTML paragraph tag<p>...</p>This makes the text look more like a structured paragraph. If you want the text to automatically form paragraphs and keep the line breaks within the paragraphs, thenlinebreaksis more appropriate.

Which filter to choose depends on the structure and style you want the content to be presented in.

Does this method apply to all multi-line text fields? Such as custom fields?

Yes, this method applies to any variable that stores multi-line text content in AnQiCMS, whether it is built-in or notarchive.Description/category.ContentOr you can customize the text field in the content model (textareafield. As long as the value of the variable contains\na newline character, you can use itlinebreaksbr|safeorlinebreaks|safeA filter to convert it to an HTML line break.

Related articles

How to set a default thumbnail in AnQiCMS to ensure that articles without uploaded thumbnails can still display images?

It is crucial to maintain the visual appeal and consistency of content in website operation.Many times, the articles we publish may not have a specific picture, or the editor may have missed uploading the thumbnail, which may lead to blank pages or inconsistent styles.AnQiCMS (AnQiCMS) has fully considered this user requirement and provided flexible default thumbnail setting features to ensure that even articles without individually uploaded thumbnails can have beautiful image displays.Why do you need to set a default thumbnail?\n\nA default thumbnail is like a "backup plan" for a website.

2025-11-08

How to configure and display structured data (Json-LD) in AnQiCMS to enhance search engine understanding?

In today's digital world, search engines are the main way users discover website content.In order to help search engines better "understand" the content on our website, structured data plays a crucial role.Json-LD (JSON for Linking Data) is a lightweight, easy-to-implement standardized data format that can clearly describe web content in a structured way, thereby enhancing the visibility and display effects of websites in search results.AnQiCMS as a content management system focusing on SEO optimization

2025-11-08

How to set up a canonical link (CanonicalUrl) in article content and ensure it displays correctly on the page?

In website operation, we often encounter situations where content is similar or repetitive, which may not only scatter the search engine's crawling budget, but also dilute the page weight, affecting SEO performance.Fortunately, AnQi CMS provides us with the function to set canonical links (Canonical URL), which can effectively help us solve these problems and clearly inform search engines which page is the 'original' or 'preferred' version of the content.The value of understanding the significance of canonical URL in simple terms

2025-11-08

How to traverse an array and control display style with loop tags (for) in AnQiCMS templates?

During website operation, we often need to display various list data, such as article lists, product lists, image galleries, or navigation menus.AnQiCMS (AnQiCMS) provides a powerful mechanism based on the Go language template engine, where the `for` loop tag is the core tool to meet this requirement.By flexibly using the `for` tag and its auxiliary functions, we can easily traverse array or list data and accurately control the display style of each element according to actual needs.

2025-11-08

How to configure custom parameters in the background of AnQiCMS and flexibly call and display them in the front-end template?

In website operation, we often encounter the need to add personalized information outside of standard fields for content or the global website.This information may be a description of the unique features of a website, product parameters, specific attributes of an article, or even multi-channel contact information.AnQiCMS (AnQiCMS) fully understands the importance of this flexibility, and therefore provides powerful custom parameter configuration features, allowing you to easily define the required data in the background and flexibly call and display it in the front-end template.

2025-11-08

How to customize the display fields of an Anqi CMS content model to meet specific business requirements?

In website operation, the diversity and personalization of content are the key to attracting users and improving efficiency.AnQiCMS (AnQiCMS) understands this and therefore provides powerful content model customization features, allowing us to flexibly adjust the display fields of content according to specific business needs.This article will take you in-depth to understand how to use this powerful feature to create a content management experience that is more tailored to your business scenario.Why do we need to customize content model fields?You may encounter such a situation: The default "article" or "product" model is general, but in actual operation

2025-11-08

How to implement personalized front-end display for different types of content (articles, products, single pages) in AnQi CMS?

In AnQi CMS, achieving personalized front-end display of different types of content (such as articles, products, and single pages) is one of its core advantages, and it is also a key factor for content operators to achieve brand differentiation and improve user experience.This is mainly due to the flexible content model design and powerful template customization capabilities of Anqi CMS. ### Flexible Content Model: The Foundation of Personalized Display The core of Anqi CMS lies in its "flexible content model".This means you are not limited to fixed "article" or "product" types, but can customize various content models according to your actual business needs. For example

2025-11-08

On the document detail page, how to call and display the custom model field content of the article?

The flexible content model of AnQiCMS (AnQiCMS) is a very powerful tool that allows us to add dedicated fields for different types of content (such as articles, products, events, etc.) based on the actual needs of the website.This means that our content is no longer limited to fixed titles, summaries, and content, but can carry more diverse and structured information.How do we call and display the content of these custom model fields on the document detail page? Next, let's explore the various flexible ways provided by Anqi CMS.###

2025-11-08