How to convert newline characters in multi-line text to HTML tags in AnqiCMS?

Calendar 78

In website content operations, we often need to input multiline text in articles, product descriptions, or page content.However, when displaying text with line breaks directly on a webpage, browsers often do not retain these line breaks as expected, but render them as continuous text on a single line, which undoubtedly affects the reading experience and layout of the content.

AnQi CMS understands this and provides very convenient built-in functions to help us easily convert line breaks in multi-line text to corresponding HTML tags, allowing content to be perfectly presented on the front-end page.

Utilize a powerful filter to convert line breaks

The AnQiCMS template engine supports a series of practical filters (Filters), one of which is specifically designed to handle line breaks in multi-line text.linebreaksandlinebreaksbrThey allow us to intelligently convert line breaks in plain text to HTML tags as needed.

linebreaksFilter

This filter will intelligently convert single-line newline characters (\nConvert to HTML's<br/>tags, and convert consecutive two or more newline characters (usually indicating a new paragraph) to<p>and</p>The tag wraps it up. It is very suitable for handling scenarios where structured paragraph display is needed while also wanting to retain the natural line breaks in the text.

For example, if our article abstract (archive.Description)or page content(page.Content)Contain multiple lines, and we want them to be displayed as paragraphs, we can use them like this:“

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

linebreaksbrFilter

If you just want to replace all line breaks with<br/>tags without generating<p>paragraph tags, thenlinebreaksbrThe filter would be a more appropriate choice. This is very useful in scenarios where strict paragraph structure is not required, but line breaks in text must be maintained, such as contact addresses, brief list descriptions, etc.

For example, contact information (contact.Address) Field may contain multi-line address information, we just want to display them simply by line:

<div class="contact-address">
    {{ contact.Address|linebreaksbr|safe }}
</div>

Understanding|safeThe importance of the filter

It should be emphasized that when using these filters, we usually add one immediately afterwards|safeFilter. This is because the Anqie CMS template engine, for security considerations, defaults to escaping all output content to prevent potential XSS attacks.If we hopelinebreaksorlinebreaksbrThe HTML tags converted by the filter (such as<p>and<br/>) It must be explicitly told to the system that this content is safe and can be parsed, so that it can be rendered normally by the browser and not displayed as raw text.|safeThis is the 'permit', which tells the template engine that this content has been reviewed and confirmed as safe HTML and can be output directly.

Another solution: Markdown editor

In addition to using the filter to process the line breaks in existing text, Anqi CMS also provides a powerful Markdown editor feature.If your content is written in Markdown format in the background, the system will automatically convert the Markdown syntax (including line breaks, headings, lists, etc.) to the corresponding HTML tags when saved and rendered.This means that as long as you use the Markdown editor to write content in the background, formatting issues such as line breaks will be automatically resolved without manually adding them to the templatelinebreaksorlinebreaksbrfilter.

In addition, if you have turned off the Markdown editor in the background, but the content itself is stored in Markdown format, or some custom field content is Markdown, you can still give it in the template byContentadd a fieldrender=trueParameters, to manually trigger the Markdown to HTML conversion. For example:

<div>
    {% archiveDetail archiveContent with name="Content" render=true %}{{archiveContent|safe}}</div>
</div>

Application scenarios in practice

These features are very practical in all content modules of Anqi CMS. Whether it is articles (archive(The main text)Content(Or summary)Description), Single page(page) Detailed content, category(category) Detailed introduction, or content model(moduleDefine the multi-line text type custom field in the following way, all these methods can be used to optimize the display of text.

BylinebreaksandlinebreaksbrThese two concise and powerful filters, or directly using the built-in Markdown editor, Anqi CMS gives us great flexibility in content creation and display.The problem of line breaks in multi-line text is easily solved, and our website content has become more beautiful and readable as a result.


Frequently Asked Questions (FAQ)

1. ApplylinebreaksorlinebreaksbrWhy are there still HTML tags in the text? Or, how can I only keep the line breaks and remove all HTML tags?

linebreaksandlinebreaksbrThe filter mainly handles the newline characters within the text itself. If your text content already contains HTML tags (such as copied and pasted rich text from elsewhere), these filters will not remove them.If you want to remove all HTML tags while preserving line breaks, you can usestriptagsFilter, for example{{ your_content|striptags|linebreaksbr|safe }}Please note the order of processing, remove tags first, and then handle line breaks, which usually results in purer text.

2. Besides the content of the article, where else can these line break conversion features be used?

These filters are widely used, almost applicable to any field that stores multiline text in AnQi CMS. In addition to the article content (Content) and summary (Description), you can also use them in the following scenarios:

  • Single page content(pageDetaillabel'sContentfield).
  • Detailed classification introduction(categoryDetaillabel'sContentorDescriptionfield).
  • Custom content modelField defined as multi-line text type in
  • Contact InformationSettings, such as contact address (contactlabel'sAddressfield).

Any multi-line text you enter in the background and want to display with line breaks on the front end can try using these filters.

3. I have already used.|linebreaks|safeBut there is still a problem with the line break display on the front-end page, what is the reason for this?

If you have already ensured that the correct filter has been used and added|safeBut the line break did not take effect, and there may be several reasons for this:

  • The content itself does not have an actual line break:Check if you actually pressed the Enter key to create a line break when typing in the background, not just by manually entering spaces.
  • CSS style effects:Your website's CSS style may override the default<p>or<br/>The display behavior of tags, for examplewhite-spaceProperties may be set tonowraporpre-lineWait, this may affect the line break display. You can check the relevant CSS rules.
  • Nested tag issue: The content may be wrapped by other parent HTML tags, and the styles of these parent tags limit the line breaks of child elements.
  • Template cache:Clear the AnQi CMS system cache and your browser cache to ensure that the latest template code and content are loaded.

Related articles

How to truncate strings or HTML content in templates and add ellipses?

When operating a website, we often encounter such situations: the summary of an article is too long, causing the page layout to be disordered;The product description is detailed but seems redundant on the list page;Or it might be a title that is too long, taking up space for other elements.These seemingly minor issues can affect the user experience and the overall beauty of the page.Fortunately, AnQiCMS (AnQiCMS) has provided us with very convenient and powerful template tags, which can easily solve the needs of content truncation and adding ellipses, making our website content display more appropriate and professional

2025-11-08

How to convert string case or capitalize the first letter in SafeCMS?

In website content operation, the way text is presented often affects the user's reading experience and brand image.In order to unify brand style, improve the readability of the title, or highlight text in specific scenarios, it is a common requirement to convert strings to uppercase or capitalize the first letter.AnQiCMS (AnQiCMS) with its flexible Django template engine syntax, provides us with several concise and efficient methods for handling these string formatting tasks.These operations do not require complex backend code modifications, just apply the corresponding filters in the template file, and you can easily achieve it

2025-11-08

How to set custom URL aliases for articles, categories, and single pages?

On the day when content management is increasingly important, a clear and friendly URL address can not only help search engines better understand the content of the page, improve the SEO performance of the website, but also provide users with a better browsing experience, and even help in brand promotion.AnQiCMS understands this and provides a powerful and flexible custom URL alias feature, allowing you to easily set personalized addresses for articles, categories, single pages, and even tags on your website.Next, we will explore how to finely manage these URL aliases in AnQiCMS.### Core Premise

2025-11-08

How to customize URL rewrite rules in AnQi CMS to optimize SEO?

In website operation, the website structure is not just a string of characters for accessing pages, but also a key factor in the website's performance in search engines.A clear, meaningful, and search engine-friendly URL (Uniform Resource Locator) that can significantly improve the SEO effect of the website, helping the content to be discovered and understood more easily.AnQiCMS (AnQiCMS) knows this and therefore integrated powerful pseudo-static and URL customization features into the system design from the beginning, allowing website operators to flexibly configure URL rules to meet different SEO strategy needs.

2025-11-08

How to automatically convert a URL string to a clickable link in a template?

In website content operation, we often encounter such situations: articles, introductions, or comments contain some URL strings that can be seen by users but cannot be directly clicked to jump, which not only affects user experience but also reduces the readability and practicality of the content.As an AnQiCMS user, you will find that the system provides us with a set of elegant and efficient solutions that can easily convert these ordinary URL strings into clickable links.AnQiCMS's template engine is powerful and flexible, it draws on the concise syntax of Django templates

2025-11-08

How to format numbers in Anqi CMS (such as retaining decimal places for floating-point numbers)?

In website operation, the way data is displayed is crucial to user experience.Whether it is product prices, statistics, or financial statements, clear and standardized number formats can not only enhance the professionalism of the website but also help users understand information more intuitively.AnQi CMS knows this need, and provides powerful number formatting functions in the template system, allowing us to easily control the decimal places of floating-point numbers, the display style of integers, and more customized presentations of numbers.### Why is Number Formatting So Important? Imagine a e-commerce website where the price of a product is displayed as "199"

2025-11-08

How to remove leading and trailing spaces or specified characters from a string?

In website content management, string formatting is often a problem we need to solve when displaying content and handling data.Especially in user input, data import, or system-generated content, strings may have extraneous spaces at the beginning and end, or specific characters may need to be removed to meet expected display effects or data standards.AnQiCMS as an efficient and flexible content management system, through its powerful template engine and rich filters, provides us with concise and powerful string processing capabilities, allowing such tasks to be easily completed at the template level.

2025-11-08

How to perform simple number or string concatenation in AnqiCMS template?

In Anqi CMS template design, flexibly handling and displaying data is the key to building a dynamic website.This is a common need in daily development to concatenate numbers or strings.AnQi CMS provides various ways to achieve this goal, from simple direct combinations to powerful filters, which can meet your different content assembly scenarios.Understanding the Basics of Concatenation: Direct Output Equivalent to "Addition" In Anqi CMS template syntax, the most intuitive way to concatenate strings is to place multiple variables or text directly side by side. For example

2025-11-08