How to make the multiline text field in Anqin CMS automatically display as a paragraph with HTML formatting?

Calendar 👁️ 54

When managing website content in Anqi CMS, we often encounter such needs: to customize the multi-line text field in the content model, hoping it can automatically display on the front-end page in a format with HTML paragraphs (such as<p>Labels display elegantly, rather than simple text stacking. This not only enhances the readability of the content but also makes the website layout more professional.

AnQi CMS provides a powerful template engine and flexible filter functions, which can easily achieve this goal.Below, we will discuss in detail how to intelligently convert multi-line text fields to HTML paragraph format.

Understanding the two main situations of content input.

In the Anqi CMS backend, when you add a 'Multiline Text' field to a custom content model (such as creating in the 'Content Model' management, refer to thehelp-content-module.mdThe content input method usually has two types:

  1. Plain text input:Users directly input content in the text box and use the enter key for segmentation. In this case, the backend usually saves it with line breaks:\nPlain text.
  2. Rich text or Markdown input:If you have enabled the Markdown editor in the background (you can configure it in "Global Settings" -> "Content Settings", see details for "help-markdown.md),users can use Markdown syntax to write content, such as using empty lines to create paragraphs, or using#/*Formatting with equal signs. The backend will save it in Markdown format.

Understanding these two input methods is the key to choosing the correct display strategy.

Use template filters to display HTML paragraph formatting

No matter whether your multi-line text field is plain text or Markdown, Anqi CMS provides corresponding filters to help us convert it into content with HTML paragraph formatting. These filters (can bedesign-tag.mdandtag-filters.mdMore information can be found here) can process the original text and generate the HTML structure you expect.

1. For plain text input: uselinebreaksFilter

When your multiline text field contains plain text and you want each newline to be automatically converted to an HTML paragraph (<p>tag), or at least a simple newline (<br/>Label, thenlinebreaksorlinebreaksbrThe filter is your ideal choice.

  • linebreaksFilter:It will convert each single newline to<br/>, and will convert two consecutive newlines (i.e., blank lines) to a new<p>Labelled paragraph. This is very useful for those who want to distinguish paragraph text by using blank lines.
  • linebreaksbrFilter:Its behavior is simpler and more direct, converting each newline character directly.<br/>without creating any<p>Label. This is suitable for scenarios where only simple line breaks are needed without emphasizing the paragraph structure.

Important note: Make sure to use|safeFilter

No matter which filter you choose, you must follow it with a|safeFilter, for example{{ archive.Content|linebreaks|safe }}This is because Anq CMS, for security reasons, defaults to escaping all content output through templates to prevent XSS attacks. If you do not use|safeEven thoughlinebreaksGenerated<p>or<br/>Tags, they will also be escaped as&lt;p&gt;and&lt;br/&gt;and will be displayed as raw text on the page, rather than being parsed as HTML elements by the browser.|safeTell the template engine explicitly: This content is safe, no escaping is required, and it can be output directly as HTML.

Usage example:

Assume that your multi-line text field is namedContentand stored inarchiveIn the object, you can call it like this in the article detail page template:

{# 假设archive.Content是纯文本,通过回车分段 #}
<div class="article-content">
    {{ archive.Content|linebreaks|safe }}
</div>

{# 如果只需要简单的换行,不生成P标签 #}
<div class="article-description">
    {{ archive.Description|linebreaksbr|safe }}
</div>

(More aboutlinebreaksandlinebreaksbrUsage, you can checkfilter-linebreaks.md)

2. For Markdown input: userenderFilter

If you have enabled the Markdown editor in the background and your multi-line text field content is written in Markdown syntax, then Anqi CMS provides a namedrenderThe filter, specifically used to convert Markdown formatted content to HTML.

Usage example:

Similarly, assuming your multi-line text field is namedContentand stored inarchiveIn the object, and you have enabled the Markdown editor in the background "Global Settings" -> "Content Settings".

{# 假设archive.Content是Markdown格式的文本 #}
<div class="article-body">
    {{ archive.Content|render|safe }}
</div>

ThisrenderThe filter will parse Markdown syntax and convert it to standard HTML structure, including converting Markdown paragraphs to<p>tags, titles to<h1>to<h6>converted to a list<ul>or<ol>and so on,|safeThe filter is indispensable here to ensure that the generated HTML can be correctly rendered by the browser.

(More aboutrenderFor information about the filter, refer tofilter-render.mdandtag-/anqiapi-archive/142.htmlabout,Contentfield'srenderParameter description.)

Combine with actual: inarchiveDetailApplication in tags such as

In the actual template creation, you usually call these multi-line text fields in the document details(archiveDetail), category details (categoryDetail) or single page details (pageDetail). Inside these tags, you can call these multi-line text fields.archiveDetailFor example, its content fieldContentsupports throughrenderparameter for Markdown conversion, but using it directly{{archive.Content}}with a filter will be more flexible:

{% archiveDetail archiveContent with name="Content" %}
<div class="post-content">
    {# 如果是Markdown内容,使用render过滤器 #}
    {{ archiveContent|render|safe }}
    {# 如果是纯文本内容,使用linebreaks过滤器 #}
    {# {{ archiveContent|linebreaks|safe }} #}
</div>

You can choose the most suitable filter according to your actual backend content input habits.

By reasonably utilizing these filters, you can flexibly control the display effect of the multi-line text field according to the actual content input, whether it is simple paragraph line breaks or complex Markdown formatting, it can present elegant and professional HTML formats on the website front end.


Frequently Asked Questions (FAQ)

1. Why did I use the tag in the template|linebreaksor|renderBut the original text still displays on the page without HTML formatting?The most common reason is that you forgot to add it at the end of the filter chain.|safeFilter. Aqiyu CMS, for security reasons, defaults to escaping all variable content output through templates. If not|safe,<p>and<br/>the following HTML tags will be escaped as&lt;p&gt;and&lt;br/&gt;, the browser will not be able to parse it as a real HTML element. Be sure to make sure your code looks like this:{{ 您的变量|过滤器|safe }}.

2.linebreaksandlinebreaksbrWhat are the differences between the filters? Which one should I choose? linebreaksThe filter will replace a single newline character with\nConvert to HTML's<br/>Labels, while two consecutive line breaks (

Related articles

Will the `linebreaks` filter still work after enabling the Markdown editor in AnqiCMS?

When we manage content in Anqi CMS, the `Content` field is undoubtedly the core of our daily operations.For many content creators, formatting text is the key to expressing ideas.Anqi CMS provides traditional rich text editors and more modern Markdown editors, which handle content in different ways.This raises a question that many people may be concerned about: Will the `linebreaks` filter we commonly use still work when the `Content` field is enabled with the Markdown editor?To understand this point

2025-11-08

The `linebreaks` filter generates multiple empty P tags when processing consecutive blank lines, does it?

When publishing content and designing templates in AnQi CMS, it is a common requirement to display the text input from the background with line breaks and spaces in the front-end page as structured HTML.The `linebreaks` filter is designed for this purpose.However, during use, many users may be curious about how the `linebreaks` filter will handle continuous blank lines in the text, whether it will generate multiple empty `<p>` tags as a result?Get a deep understanding of the `linebreaks` filter's working mechanism

2025-11-08

My website comment content has multiple lines of text, how to use the Anqi CMS filter to automatically add HTML line numbers to each line?

In website operation, user comments are an important manifestation of community activity.When the comment content is long, especially when it contains multiple lines of text, the user may find it inconvenient to read or quote specific content.At this time, automatically adding line numbers to the comment content can significantly improve readability, making it convenient for users to communicate and refer to specific lines, greatly optimizing the user experience.The Anqi CMS, with its efficient architecture based on Go language and flexible Django-style template engine, provides powerful customization capabilities for content display.

2025-11-08

In AnQiCMS, when using the `linebreaksbr` filter, does it only convert line breaks to `<br/>`?

In AnQiCMS template development, we often need to display some user input plain text content, such as product descriptions, article summaries, etc., on the web page in a way that retains the original line break format.To achieve this purpose, AnQiCMS has built-in a series of practical filters, among which `linebreaksbr` is a tool specifically designed to handle line breaks.However, many users may have a question about this filter: Does it really only responsible for simply converting newline characters to the HTML `<br/>` tag?Today, let's delve deep into this issue

2025-11-08

How to customize the line number style or prefix generated by the `linenumbers` filter in AnQiCMS template?

In AnQiCMS template development, the `linenumbers` filter is a very practical tool that can help us automatically add line numbers to multi-line text content.This is very convenient when displaying code snippets, referencing specific lines of text, or when analyzing content line by line.How does the AnQiCMS template system support adjusting the styles or changing the prefix of the generated line numbers?First, let's review the basic usage of the `linenumbers` filter

2025-11-08

Use the `linebreaks` filter to convert HTML content, do you need to combine it with the `|safe` filter to output?

When using Anqi CMS to display website content, the flexibility and security of the template are the focuses of developers.AnQiCMS's template engine provides a rich set of filters for content processing, among which the `linebreaks` and `|safe` filters often appear together and often raise questions among developers who are new to the field: After `linebreaks` has already converted plain text to HTML content, is it still necessary to combine it with the `|safe` filter for output?This article will delve deeply into this issue. ###

2025-11-08

Why did I use the `linebreaks` filter, but the multiline text is still not converted to HTML tags?

Many AnQiCMS users may encounter a situation during template development: Even though they have used the `linebreaks` filter for multiline text in the template, expecting it to automatically recognize and convert newline characters in the text to HTML paragraph (`<p>`) or break (`<br/>`) tags, the text is still displayed on the page with literal HTML tags, rather than the expected parsed effect by the browser. This is indeed perplexing, but in fact, the problem usually arises from some misunderstandings about the default behavior of the AnQiCMS template engine.

2025-11-08

How to avoid users entering multiline text in the AnQi CMS comments or messages, causing front-end layout confusion?

When operating a website, the comment section or message board is often an important bridge for users to interact with the website.Users share ideas, ask questions here, bringing vitality to the website.However, when users input multiline text in comments or messages, if it is not properly processed, this content is likely to lead to disordered page layout on the front-end, affecting the overall aesthetics and user experience of the website.This content will discuss how to elegantly solve this problem in AnQi CMS.## Understanding the Root Cause of Messy Multi-line Text Formatting Users in comment or message boxes (usually <textarea>

2025-11-08