How to implement line break and line number display for multi-line text in AnQiCMS template?

Calendar 👁️ 69

When building a website with AnQiCMS, we often need to display multi-line text, such as article content, product feature descriptions, code snippets, and even comments.How to properly implement line breaks for this multiline text on the page, and further add line numbers to certain specific content (such as code), is the key to improving reading experience.Luckyly, AnQiCMS's powerful template engine provides a very convenient solution, and we can easily implement these features by using built-in filters.

Understanding multi-line text in AnQiCMS

In the AnQiCMS backend, when you enter multiline text in the content editor (such as in the "Publish Document" interface, or in the "Multiline Text" field created in a custom content model) and press the Enter key, the system will store it as containing a line break (\nThe plain text. However, standard HTML renders multiple consecutive spaces, tabs, and new lines as a single space.This means that if you output unprocessed text directly in the template, the browser will not display the line breaks you enter in the background.

For example, if you enter in the background:

这是第一行。
这是第二行。

这是第四行(第三行为空行)。

Use it directly in the template:{{ archive.Content }}The output, the browser will probably only display as:这是第一行。 这是第二行。 这是第四行(第三行为空行)。

To solve this problem, AnQiCMS provides a special filter to convert the newline characters in the text to the HTML line break tags that browsers can recognize.

Implement text line break processing

The AnQiCMS template provides two commonly used filters for handling the line break requirements of multi-line text, each with its own focus, and can be flexibly selected according to the actual situation.

1. Basic newline: UselinebreaksbrFilter

linebreaksbrThe filter is the most direct way to handle newlines. Its role is to replace each independent newline in the text with\nsimply to the HTML's<br/>Label. This is very suitable for those who only want to achieve the natural vertical separation of text, without forming a strict HTML paragraph structure, such as some brief descriptions, list items, or address information.

The method of use is very intuitive, you just need to add it after the variable you need to process|linebreaksbrThe filter can be used. Since this filter generates HTML tags, in order for the browser to correctly parse and display these tags, you also need to add it at the end of the filter chain.|safefilter.

{# 假设有一个多行文本变量 `item.Description` #}
<div>
    {{ item.Description | linebreaksbr | safe }}
</div>

{# 如果是文档内容字段,且后台未开启Markdown处理 #}
<article>
    <h1>{{ archive.Title }}</h1>
    <div>
        {{ archive.Content | linebreaksbr | safe }}
    </div>
</article>

After such processing, every line of text you input in the background will be displayed independently on the front-end page, achieving clear line breaks.

2. Paragraph-style line breaks: uselinebreaksFilter

If your need is more inclined towards structured paragraph display,linebreaksthe filter will be a better choice. It will not only replace single line breaks with<br/>Labels will also replace consecutive two or more newline characters (usually representing a blank line) with HTML's<p>and</p>Label. This helps to automatically organize long text content into paragraphs that conform to HTML semantics, which is particularly suitable for content that requires good reading structure, such as article text and product descriptions.

withlinebreaksbrsimilar,linebreaksThe filter also needs to be with|safeThe filter is used in conjunction to ensure that the generated HTML tags can be correctly rendered by the browser.

{# 假设有一个多行文本变量 `longText` #}
<section>
    {{ longText | linebreaks | safe }}
</section>

{# 如果是文档内容字段,且后台未开启Markdown处理 #}
<article>
    <h1>{{ archive.Title }}</h1>
    <div class="article-content">
        {{ archive.Content | linebreaks | safe }}
    </div>
</article>

UselinebreaksAfter the filter, your multiline text will be presented in a more standardized paragraph format, enhancing the readability and page structure.

Implement line number display

For scenarios that require line numbering, such as displaying code examples, log outputs, or detailed step lists, AnQiCMS provideslinenumbersA filter that automatically adds line number prefixes to each line of text, the default format is1./2.etc.

linenumbersFilters are usually used in conjunction with other line break filters to ensure that line numbers are correctly matched with each line of content. Similarly, since it generates HTML structure (for example<pre>and<code>Labels, as well as line numbers), you still need to add|safeFilters to ensure that the browser correctly parses.

{# 假设有一个代码片段存储在 `archive.CodeSnippet` 自定义字段中 #}
<pre class="code-block">
    <code>{{ archive.CodeSnippet | linenumbers | safe }}</code>
</pre>

{# 也可以结合换行过滤器,确保文本在添加行号前已经有正确的换行结构 #}
<div class="log-output">
    {{ archive.LogContent | linebreaksbr | linenumbers | safe }}
</div>

BylinenumbersFilter, you can make those multi-line plain text content come alive, convenient for readers to track and refer to specific lines.

Considering the actual application scenarios

  • Content and Markdown:AnQiCMS supports enabling the Markdown editor to write article content. If your content (such asarchive.ContentIt is written through a Markdown editor, then Markdown itself will handle the line breaks and paragraph structure of the text and convert it to HTML. In this case, you can directly uselinebreaksorlinebreaksbrThe filter may not be necessary, and it may even produce unintended effects. However,linenumbersThe filter can still number the plain text content (i.e., visible text lines) converted from Markdown, which may be useful in certain special layouts.
  • code snippet: We recommend creating a special "Multi" in the content model for cases where code needs to be displayed on the website

Related articles

How to customize structured data with Json-LD tags in AnQiCMS to optimize the display of search results?

AnQiCMS: Custom JSON-LD structured data, the secret weapon to light up search results In today's fiercely competitive information ocean, making our website content better understood and displayed by search engines is the key to improving online visibility.Structured data, especially JSON-LD, is playing an important role.It can help search engines accurately interpret page information, thereby presenting it in a more rich and attractive form in search results, which is what we commonly refer to as "Rich Snippets".AnQiCMS knows this

2025-11-08

How to ensure that the custom system parameters set in the AnQiCMS backend can be correctly displayed in the frontend template?

The flexible application of custom system parameters in Anqi CMS is the key to improving website maintainability and operational efficiency.Many times, we may need some information that is not covered by the default system fields, such as specific external links, additional corporate honor information, or exclusive copywriting for a specific event.AnQi CMS provides a convenient way to add these custom parameters and ensures they are presented accurately in the front-end template.### One, understand the setting location of custom system parameters Firstly, we need to clarify the setting entry of custom system parameters in the Anqi CMS background

2025-11-08

How to display the current year or a custom time format in AnQiCMS templates?

In website operation, the accuracy and display of time information has a significant impact on user experience and content professionalism.Whether it is the copyright year in the footer or the publishing or updating time of the article content, a clear and consistent date format can enhance the overall perception of the website.AnQiCMS provides very flexible and powerful features in templates to meet these time display requirements.### Show the current year: Use the `{% now %}` tag to quickly display Many website footers display the current year as part of the copyright statement, for example “© 2023

2025-11-08

How to split a string into an array with a specified delimiter and iterate through it in AnQiCMS?

In website operation, we often encounter the need to store some seemingly simple, but actually containing multiple pieces of information data in an article, a product description, or some custom field.For example, multiple tags of a blog, multiple features list of a product, or some dynamic display related words.These data are usually connected into a long string with a specific delimiter (such as a comma, semicolon, or pipe).

2025-11-08

How to use AnQiCMS's `cut` filter to accurately remove all specific special characters from the article title?

In website content operation, we often hope that the article title can accurately convey information and maintain visual neatness and professionalism.However, sometimes due to specific requirements or content import, some unnecessary special characters may appear in the title, which not only affect the aesthetics but may also cause layout confusion in some display scenarios.For this common question, AnQiCMS provides a very practical template filter —— `cut`, which can help us accurately remove all specific special characters from the article title, making the title look fresh.### Understand `cut`

2025-11-08

In AnQiCMS template, can the `cut` filter batch remove all spaces from the product name to optimize the URL or search?

As content operators, we all know that a clear and friendly URL is crucial for the SEO performance and user experience of a website.How to ensure that spaces in product names do not affect the URL structure or search results when displaying products or articles, which is a common concern for everyone.AnQiCMS (AnQiCMS) relies on its flexible template engine to provide a rich set of filters to handle such requirements.Today, let's delve into whether the `cut` filter in AnQiCMS templates can batch remove all spaces from product names

2025-11-08

Can I remove all HTML comments from the user submitted content in the template using the `cut` filter?

In website operation, we often encounter the need to process user submitted content to ensure the neat and standardized display of the website front-end.One common requirement is to remove HTML comments from the content.When users may paste content from various sources, these hidden comments often come along with it, although they are not visible on the page, they may increase the page loading burden, and even affect the layout in some special cases.Users may think of the `cut` filter in AnQiCMS templates, intuitively feeling that it can "cut off" the unnecessary parts. However

2025-11-08

How to use the `cut` filter to remove the `?` symbol and its subsequent parameters from the image URL path to obtain a pure path?

In website operation, the management and display of image resources are crucial.To ensure the website loads quickly, improve search engine optimization (SEO) and optimize the user experience, we often need to handle image URLs.Sometimes, an image URL may carry some parameters, such as version numbers, cropping sizes, or tracking information, which are usually followed by a question mark (`?')`Start. Although these parameters have their specific purposes, in certain display scenarios, we may wish to obtain a pure image path without any parameters.In AnQiCMS (AnQiCMS)

2025-11-08