How to automatically convert multiline text content to a tag with

or
HTML format for display?

Calendar 👁️ 76

In website content display, we often encounter such needs: multi-line text edited in the background can be automatically converted to paragraphs with HTML tags on the front-end page<p>or newline tag<br>The format, rather than being simply crammed together into a long string of text.AnQiCMS provides a flexible and powerful method to solve this problem, whether it is through the built-in editor features or by using template filters for fine control, it can be easily achieved.

One, use the content editor to enable automatic conversion

When you create or edit articles, product details, single pages, or category content through the AnQiCMS backend, you will usually use the content editor.These editors (including rich text editors and Markdown editors) inherently have the ability to automatically convert the line breaks you enter into the corresponding HTML tags.

If you have enabled the Markdown editor in the background (you can do so in theGlobal settings -> Content settingsFind and enable it, then the newline characters you enter, especially two consecutive newlines, will be automatically recognized and rendered as HTML paragraph tags<p>. And a single newline character may be converted to<br/>. This means that as long as you are in the background content field (for example文档内容/页面内容or分类内容Normal segmentation and line breaks are performed, when AnQiCMS displays this content in the front-end template, it will intelligently convert it to HTML format with<p>or<br/>tags.

For example, when calling document content in your template, you will usually use something like{% archiveDetail articleContent with name="Content" %}{{articleContent|safe}}</div>tags. If the content is entered through a Markdown editor,articleContentThe variable will automatically include the converted HTML structure. Among which the|safeThe filter is crucial, it tells the template engine that this content is safe HTML, does not require additional escaping, so<p>and<br/>it can be rendered normally in the browser.

Second, use template filters for fine control

In addition to the automatic conversion of the editor, AnQiCMS also provides powerful template filters that allow you to perform more detailed HTML formatting conversions on multiline plain text content.This is particularly useful when handling plain text data obtained directly from a custom multi-line text field without going through a content editor.

1.linebreaksFilter: Smart paragraph and line break processing

When you want to convert the paragraph format of plain text into standard HTML paragraph tags<p>Keep the line breaks within the text, please.linebreaksThe filter is your ideal choice. It works in a way similar to many blogging systems: it recognizes consecutive two newline characters (i.e., blank lines) as a paragraph end and uses<p>...</p>Enclosed in tags. While individual line breaks are converted to<br/>.

This method is very suitable for converting user input, unformatted multi-line comments, product descriptions, or service descriptions into HTML format with paragraph structure and retained internal line breaks.

For example, if you have a custom multi-line text field namedproduct_descriptionyou can call and handle it in the template like this:

{# 假设 product.product_description 变量中包含多行纯文本内容 #}
<div class="product-description">
    {{ product.product_description|linebreaks|safe }}
</div>

Remember to do this here|safeThe filter is also indispensable, it ensures thatlinebreaksThe HTML tags generated by the filter can be correctly parsed and displayed by the browser.

2.linebreaksbrFilter: Simple line break conversion

If your requirement is simpler, you just want to replace each newline in plain text with the HTML<br/>tag without needing the paragraph tag<p>thenlinebreaksbrThe filter will be more direct and lightweight. It will not judge blank lines and paragraphs, but insert one when it encounters a newline.<br/>.

This method is suitable for scenarios where you need to keep the text compactly aligned but also want to have a simple line break between each natural paragraph or list item, such as address information, contact list, and brief points of explanation, etc.

For example, if you have a custom multi-line text field in the contact information settings to display the company address, you can call it like this:

{# 假设 contact.address_lines 变量中包含多行纯文本地址信息 #}
<address>
    {{ contact.address_lines|linebreaksbr|safe }}
</address>

Similarly,|safeThe filter ensures<br/>The key to correct rendering of labels.

How to choose a way that suits you?

  • For the main content area (such as article details, main body):Use the built-in AnQiCMS backend editor function first.If you are accustomed to Markdown, enabling the Markdown editor will bring a more convenient writing experience and automatic HTML conversion.
  • For custom multi-line text fields (such as product highlights, brief descriptions)If this text content requires strong paragraph sense and structure,linebreaksThe filter would be a better choice. It can convert multiple natural paragraphs into HTML.<p>Labels, make text more readable.
  • For simply separating each line of content (such as addresses, list items): If you don't need strict paragraph division, but just want each line to occupy a line by itself,linebreaksbrthe filter is more suitable, as it can convert each newline character to<br/>.

AnQiCMS through built-in editor features and flexible template filters, allows you to have a variety of choices when displaying multiline text content, ensuring that your website content is both aesthetically pleasing and semantically standard.


Frequently Asked Questions (FAQ)

Q1: Why did I uselinebreaksorlinebreaksbrFilter, but what is displayed on the page is<p>...</p>or<br/>Such original HTML tags, not actual paragraphs and line breaks?

A1:This is usually because you forgot to add after the filter.|safeFilter. The AnQiCMS template engine, for security reasons, defaults to escaping all output content to HTML, to<to&lt;etc. WhenlinebreaksorlinebreaksbrGenerate HTML tags after, if not|safeTell the template engine that these tags are safe, they will be escaped and displayed as original text. The correct way to use it is{{ 你的变量|linebreaks|safe }}or{{ 你的变量|linebreaksbr|safe }}.

Q2: I have already formatted the multi-line text in the background content editor, and I still need to use it in the templatelinebreaksfilter?

A2:Generally, it is not necessary. The rich text editor or Markdown editor on the AnQiCMS backend has already converted your formatting (including paragraphs and line breaks) into standard HTML format when saving content.When you call this content directly in the template (for example{{ archive.Content|safe }}when it will be displayed directly as formatted HTML.linebreaksorlinebreaksbrThe filter is mainly used to process those multi-line "plain text" contents that have not been processed by the editor, such as text input through custom fields.

Q3:linebreaksandlinebreaksbrWhat are the specific differences between the filters, and how should I choose?

A3:The main difference between them lies in the way they handle newline characters:

  • linebreaksFilter:More intelligent and semantic. It will recognize consecutive two newline characters (representing an empty line) as a paragraph end, and<p>...</p>Text enclosed by tags. A single newline character in the text will be converted to<br/>Tags. This is suitable for text that requires clear paragraph structure.
  • linebreaksbrFilter:It is more direct and simple. It will replace each newline character (whether single or consecutive) without discrimination, replacing it with HTML's<br/>tags without generating any<p>Tags. This is applicable to scenarios where only simple line breaks are needed, without strict paragraph division, such as addresses, list items, or poetry.

Choose which one depends on the HTML structure and visual effects you want the text to display. If you want the browser to handle paragraph spacing and maintain the semantic segmentation of the text, chooselinebreaksIf you just want to start each line of text on a new line, selectlinebreaksbr.

Related articles

How to dynamically display copyright information and the current year at the bottom of a website or other areas?

In website operations, keeping information up to date is an important part of maintaining a professional image, and the copyright year at the bottom of the website is often overlooked. Manually modifying it every year is not only cumbersome but also prone to omission.Luckyly, using Anqi CMS, we can easily implement the dynamic display of copyright information, so that the information at the bottom of the website is always up-to-date.Why is dynamic copyright information so important?Imagine a user visiting a website and seeing the copyright year prominently displayed at the bottom.This could make the website look outdated and may give people an impression of unprofessionalism.For content operators

2025-11-09

How to correctly render Markdown document content as HTML in a template?

Content creators often choose Markdown as their preferred tool because it can organize content in a concise and clear manner, while also easily achieving formatting effects.However, simply inputting content in Markdown format in the background is not enough to allow the website front end to display it correctly. We need some additional steps to ensure that the content is rendered correctly as HTML.This article will introduce in detail how to make Markdown content turn beautiful in AnQiCMS templates, presenting it to users in the form of HTML.### One

2025-11-09

How to optimize the display format of text and numbers using AnQiCMS filters (such as truncatechars, floatformat)?

How to make information concise, clear, and professional in website content operation?This is often a challenge faced by many operators. Whether it is a news summary, product description, or price data, statistics, if displayed in a disorganized manner, it not only affects user experience but may also lead to deviations in information transmission.AnQiCMS (AnQiCMS) fully understands this pain point, its powerful template engine built-in a series of practical filters, which can help us easily optimize the display format of text and numbers, making your website content look brand new.Refined text

2025-11-09

How to view the content that has been crawled and accessed in the website traffic statistics and crawler monitoring data?

Manage a website, what we care most about is whether our content is seen by users and whether it is indexed by search engines.This data is like the pulse of the website, directly reflecting the health of content operation and the activity of the website on the Internet.AnQi CMS knows these core needs and has built practical traffic statistics and spider monitoring functions, allowing us to easily master the key performance indicators of the website.Why are we so concerned about traffic and crawler data?In short, paying attention to these data is to better understand and optimize our website. First

2025-11-09

How to ensure that the URL strings in the content are automatically converted into clickable hyperlinks?

In daily website content operations, we often need to include various links in articles or descriptions, whether they point to other pages within the site or external references.One of the key aspects of user experience is that these URL strings can be automatically turned into clickable hyperlinks, rather than a series of cold texts.AnQiCMS (AnQiCMS) fully understands this and provides a variety of flexible and efficient methods to help us ensure that URLs in the content can be intelligently converted into clickable hyperlinks.Next, we will discuss how to achieve this goal in AnQiCMS

2025-11-09

How to use the 'safe output' mechanism of AnQiCMS template to avoid HTML content from being escaped?

In website content management, we often need to display various text information on the page, including HTML content with specific formats or interactive effects.AnQiCMS (AnQiCMS) is a modern content management system that, by default, takes an important security measure when handling template rendering: automatically escaping HTML content.This mechanism is designed to prevent cross-site scripting (XSS) attacks and ensure website security.However, in certain specific scenarios, such as when we want to display formatted content edited by a rich text editor, or custom HTML code snippets

2025-11-09

How do user group management and VIP systems affect the access permissions and display of specific content?

How to allow different users to see different content on a website, even charging for some high-value content, is the key to enhancing the value of the website and achieving monetization.AnQiCMS provides powerful and flexible features in user group management and VIP system, helping us achieve refined content distribution and differentiated display. ### Understanding the Core Value of User Groups and VIP Systems In Anqi CMS, user group management is the foundation for building personalized content experiences.

2025-11-09

What methods does AnQiCMS provide for template developers to debug variable output and structure type?

When developing templates in Anqi CMS, understanding and debugging variable output and data structure types is the key to improving efficiency.Although the AnQiCMS template engine (similar to Django or Blade syntax) is intuitive and easy to use, mastering some debugging techniques can make you twice as effective when encountering problems.We usually understand the variables and data structures in the template through the following ways. ### 1.

2025-11-09