How does the AnQiCMS template automatically convert newline characters in text to HTML `

` or `
` tags?

Calendar 👁️ 84

In the presentation of website content, the formatting of text is often the key to determining the user's reading experience. We often encounter such needs: the text content obtained from the background database usually only contains simple line breaks (\n),If displayed directly on a webpage, the browser will not intelligently convert these newline characters into the HTML paragraphs we expect (<p>) or line breaks (<br>The tag causes the content to be cramped together, losing its original structure and readability.

AnQiCMS (AnQiCMS) has fully considered this point in template design, providing a series of convenient built-in filters that can help us easily convert newline characters in plain text to standard HTML tags, ensuring that the content is presented naturally and beautifully on the front-end page.These filters are like intelligent formatting tools, which can easily solve complex text layout problems.

Core mechanism: Understanding AnQiCMS text processing filter

The Anqi CMS template engine adopts a syntax similar to Django, one of its core advantages is the flexible 'filter' function.The filter allows us to modify and format the output content of a variable.For line break conversion, AnQiCMS mainly provideslinebreaksandlinebreaksbrThese two filters, each suitable for different text formatting needs.

1.linebreaksFilter: Smart segmentation and inline line breaks

When processing articles, long descriptions, or comments, we expect them to be automatically segmented on the web like traditional text, with new paragraphs formed where there are blank lines, and the line breaks within paragraphs to be preserved, whenlinebreaksThe filter is an ideal choice.

linebreaksThe principle of operation is as follows:

  • Converts consecutive two newline characters(\n\n)to HTML paragraph tags<p>...</p>.This means that in the original text, if you create an empty line by pressing Enter twice,linebreaksit will be recognized as a new paragraph.
  • a single newline character (\n) to an HTML newline tag<br>.If you press the enter key once only within a paragraph, and you want the content to continue on the next line without starting a new paragraph,linebreaksit will add one for it<br>.

For example, assume that the text content transmitted from the backgrounditem.descriptionAs follows:

这是一段产品描述。
产品特点一。
产品特点二。

这是另外一段说明。
请注意相关事项。

In the template, you can use it like thislinebreaksFilter to output:

{{ item.description|linebreaks|safe }}

here,|safeThe filter is crucial. Because it tells the template engine, afterlinebreaksThe processed content is safe HTML and does not need to be default HTML entity encoding (such as changing <p>Escape to&lt;p&gt;Thus ensuring that the browser can correctly parse and render these HTML tags.

After such processing, the final HTML structure rendered on the web page will be: "`html"

This is a product description.
Product feature one.<br>

Related articles

How to get the total length of elements in a string, array, or key-value pair in AnQiCMS template?

In AnQi CMS template design, dynamically obtaining the total length of elements in a string, array, or key-value pair is a very practical feature. It can help us flexibly display information on the page, such as showing the number of products, the number of comments, or adjusting the layout according to the content length.The Anqi CMS template engine provides simple and efficient filters to meet these requirements.### Core Tool: `length` Filter The most direct and universal method to obtain the total number of elements in any string, array (slice), or key-value pair (map/struct) is to use

2025-11-08

How to concatenate the tag array in AnQiCMS template into a string with a custom delimiter?

In AnQiCMS template development, we often need to display a set of data obtained from the database, such as the multiple tags (Tags) of articles, in a user-friendly manner rather than simply listing them.One of the most common needs is to concatenate these tags into a string with a custom separator, such as AnQiCMS's powerful template engine provides a flexible way to achieve this goal.

2025-11-08

In AnQiCMS template, how to convert a string number into an actual `integer` or `float` type?

When working with the Anqi CMS template to display content and make logical judgments, we often encounter situations where we need to perform operations on numbers.However, data obtained from a database or content model, even if it looks like a number in the background, is sometimes passed as a string (``string``) at the template level.If this is directly performed arithmetic operations or numerical comparisons, unexpected results may occur.

2025-11-08

How to find the first occurrence index of a character or substring in the AnQiCMS template?

During the process of displaying website content or template development, we often encounter situations where we need to process specific text, such as checking if a keyword exists or locating the first occurrence of a character or substring.The template engine of AnQiCMS (AnQiCMS) provides a series of powerful filters (Filters) to help us complete these tasks efficiently.Today, let's discuss how to use the `index` filter to accurately find the position index of the first occurrence of a character or substring in the AnQiCMS template.Understand

2025-11-08

How to dynamically define and use small array variables for loops in AnQiCMS templates?

In AnQiCMS template development, we often need to flexibly display some small data sets according to business logic.These collections may be some custom tags, navigation items, or just some status identifiers.Although AnQiCMS provides rich tags to call background data, sometimes we want to define a small array directly in the template and loop through it to achieve finer control and simpler template code.

2025-11-08

How to convert letter combinations like 'PONGO2' into the corresponding numbers on a phone keypad?

How to easily convert letter combinations to phone number keypad digits in AnQiCMS?In daily life, we sometimes encounter some phone numbers composed of letters and numbers, for example, some companies may use memorable numbers like "999-PONGO2" for brand promotion.However, when making a phone call in reality, these letters need to be converted into the corresponding numbers on the phone keypad.For website operators, if they can automatically complete this conversion on the website front end, it will undoubtedly greatly improve the user experience.

2025-11-08

How to display the singular or plural form of a word based on the quantity value in the AnQiCMS template?

When building a website, we often encounter situations where we need to dynamically display text based on a certain quantity value.For example, when there is '1 comment', we want to display it in singular form; while for '2 comments', it should be displayed in plural form.This detail handling not only improves user experience but also shows the professionalism of the website content.If manual quantity judgment and writing if/else logic make the template code long and difficult to maintain.

2025-11-08

How to safely remove all or part of a specified tag from HTML content in AnQiCMS templates?

When building a website with AnQiCMS, we often need to finely control the content displayed on the page.Especially when dealing with user submitted content, importing articles from different sources, or simply to maintain consistency in page style, you may encounter the hassle of HTML tags.These tags may contain unnecessary formatting, styles, and even potential security risks.Fortunately, the AnQiCMS template system provides a flexible mechanism to help us safely and efficiently remove all or part of the specified tags from the HTML content.

2025-11-08