Can the text be converted using the `linebreaks` filter and then truncated with `truncatechars_html`?

Calendar 👁️ 63

In Anqi CMS template development, flexible handling of text and HTML content is a daily operation. When we need to process plain text content input by users, throughlinebreaksWhen converting a filter into an HTML structure with paragraphs and line breaks while also limiting its display length, a common problem often arises: afterlinebreaksCan the processed HTML content be used safely?truncatechars_htmlWhat if the filter truncates it?

The answer is yes, not only can it, but it is exactly thistruncatechars_htmlThe strength of filter design. Next, let's take a detailed look at the working mechanisms of these two filters, as well as how they elegantly complement each other.

linebreaksFilter: Converts plain text to HTML structure

first, linebreaksThe filter plays a very practical role in Anqi CMS templates. Its main function is to convert the newline characters in plain text into\n)<p>) and line break tags (<br/>)

Imagine if the backend editor entered multiple lines of text in the content field, separated by two newline characters between paragraphs, and simply used a single newline character within paragraphs.If not processed directly, the front-end page may only display a long string of unformatted text.linebreaksAfter the filter intervenes, it will convert such plain text content, by convention, into an easily readable HTML paragraph structure. Specifically:

  • Two consecutive newline characters will be parsed as a new HTML paragraph, wrapped by<p>...</p>.
  • A single newline character will be converted to the HTML<br/>tag to implement line breaks within the paragraph.

For example, an input like this:

这是第一段内容。
这里有段内换行。

这是第二段内容。

afterlinebreaksAfter processing, it will output something like the following HTML:

<p>这是第一段内容。<br/>这里有段内换行。</p>
<p>这是第二段内容。</p>

This conversion makes the plain text content have basic formatting and readability when displayed on the front end.

truncatechars_htmlFilter: Smartly truncate HTML

Let's take a look attruncatechars_htmlThis filter. Its core function is to truncate text based on the specified character count, but it is different from the ordinary text truncation. It can recognize and correctly handle HTML tags.This means it will not simply truncate at the character limit, causing HTML tags to not close or the structure to be destroyed.On the contrary, it will:

  • Calculate the number of visible characters, ignoring the characters occupied by the HTML tags themselves.
  • Intelligently close all open HTML tags at the truncation position, ensuring that the generated HTML is still valid and complete.
  • Automatically add an ellipsis at the end of truncated text (...Incomplete content indicated.

For example, if there is a piece of HTML content that is.<strong>安企CMS</strong>是一个<em>强大的</em>系统。We use.truncatechars_htmlTruncate it to 10 characters:.<strong>安企CMS</strong>是一个...It will close correctly.<strong>Tags, leaving no unclosed tags.

combining them:linebreakswithtruncatechars_htmlThe perfect match

Now let's return to our core issue: whenlinebreaksThe filter converts plain text to HTML,truncatechars_htmlCan it be truncated? The answer is yes, and this chained call is highly recommended.

Working principle:

  1. You provide the original plain text content, such as fromarchive.Descriptionthe field.
  2. linebreaksThe filter first processes this plain text, converting the line breaks to<p>and<br/>tags, generating anew, well-structured HTML fragment.
  3. This newly generated HTML fragment is passed totruncatechars_htmlfilter.
  4. truncatechars_htmlAfter the filter receives this HTML, it will intelligently identify and process these by its design principle,linebreaksGenerated HTML tags, while calculating the number of visible characters, ensure that all opened tags are properly closed at the truncation point (even if<p>The tag also ensures that it is properly closed). The final output is a text with a limited length but a complete HTML structure, accompanied by an ellipsis.

Here is an example:Assumearchive.DescriptionContains the following content:

安企CMS致力于提供高效解决方案。
它部署简单。
功能强大且易于扩展。

In the template, we can call it like this:

{{ archive.Description|linebreaks|truncatechars_html:30|safe }}

The output might be like this:

<p>安企CMS致力于提供高效解决方案。<br/>它部署简单。功能...</p>

You can see that even if the text contains bylinebreaksGenerated<p>and<br/>tag, truncatechars_htmlCan also handle properly, ensure that the output HTML is valid and meets the length requirements. It should be noted that due tolinebreaksandtruncatechars_htmlAll generate or process HTML, so it is necessary to add it to the output on the page|safeA filter to prevent HTML content from being escaped twice, resulting in tags being displayed directly instead of rendering their effects.

In this way, you can flexibly handle the original text entered by users in the Anqi CMS, first perform basic formatting, then elegantly truncate, so as to display standardized and beautiful content segments in scenarios such as list pages, summaries, etc.


Frequently Asked Questions (FAQ)

1. Why not use directly?truncatecharsThe filter is used to truncate text insteadtruncatechars_html? truncatecharsThe filter is designed to truncate plain text and does not recognize HTML tags. If you have gone throughlinebreaksUse the text directly on the processed (already generated HTML) texttruncatecharsIt may be cut off within HTML tags, causing the tag to be incomplete and thus damaging the HTML structure of the page, resulting in a chaotic display. Andtruncatechars_htmlIt exists just to avoid this kind of situation, it can intelligently handle HTML, ensuring that the truncated structure is complete.

2. If I want to truncate by word instead of character count and the content contains HTML, which filter should I use?AnQi CMS providestruncatewords_htmlA filter that performstruncatechars_htmlSimilar, but it is truncated by word count. Similarly, it will also intelligently identify and retain HTML structure.So, if your requirement is to truncate content containing HTML based on the number of words,truncatewords_htmlWill be your ideal choice. The method of use is the same astruncatechars_htmlfor example:{{ archive.Description|linebreaks|truncatewords_html:10|safe }}.

3. I used in the templatelinebreaksandtruncatechars_htmlbut the front-end displayed it directly<p>and<br/>tags, why is that?This is usually because you forgot to add|safeThe filter. Anqi CMS (and many template engines based on Django-like syntax) defaults to escaping all output in HTML to prevent XSS attacks. Whenlinebreaksandtruncatechars_htmlAfter generating the HTML code, if there is not|safeThese codes are safe, the template engine will treat them as plain text and escape them, causing the tags to be displayed directly. Plus|safeTell the template engine that 'this content is safe HTML and render it directly.'

Related articles

How does the `linenumbers` filter perform on the long multi-line text submitted by the user?

The AnQiCMS template system is renowned for its flexibility and high performance, including various filters that help us efficiently process and display content.When it comes to users submitting long multi-line text and needing to add line numbers, the `linenumbers` filter is a very practical tool.How does the performance of this filter actually perform in practical applications?

2025-11-08

Does the `linebreaks` filter support configuring custom paragraph tags instead of `&lt;p&gt;`?

When using AnQiCMS for content creation and template design, we often encounter situations where we need to finely control the text format.Among them, handling newline characters in text is a common requirement.The Anqi CMS provides various filters to help us achieve this goal, and the `linebreaks` filter is one of them.However, users may be curious in practice whether this filter supports converting line breaks in text to custom HTML tags instead of the default `<p>` tag?In-depth understanding

2025-11-08

How can I preview the effect of the `linebreaks` filter on multi-line text in the template?

In website content management, we often encounter situations where it is necessary to display multi-line text, such as article summaries, product descriptions, or user comments.This text may just be a simple string with line breaks in the database, if it is output directly to the web page, the browser will usually ignore these line breaks, causing all the text to be crowded together, which seriously affects the reading experience.AnQiCMS (AnQiCMS) provides a powerful template engine, and the `linebreaks` filter is designed to solve this problem, it can intelligently convert newline characters in plain text to

2025-11-08

The `Content` field in the Anqi CMS article content, when should the `linebreaks` filter be used manually?

When managing and displaying article content in Anqi CMS, the `Content` field carries the main information of the article.Understanding when and why to manually use the `linebreaks` filter is crucial for ensuring content is displayed in the expected format on the website front end.This is not a general issue, but depends on the way of editing the content and the final display requirements.### Understanding the rendering mechanism of the `Content` field Firstly, we need to clarify the two main processing methods of the `Content` field in Anqi CMS: 1.

2025-11-08

Does the rich text editor content of Anqi CMS conflict after being processed by the `linebreaks` filter?

When managing content in AnQi CMS, we often use a powerful rich text editor to create articles with vivid pictures and text.At the same time, the template engine of Anqi CMS also provides various filters to flexibly handle content, among which the `linebreaks` filter is used to handle text line breaks.Then, when the content generated by the rich text editor is processed by the `linebreaks` filter, will there be any conflicts between them? To answer this question, we need to first understand the working principles of these two functions.###

2025-11-08

How to apply `linebreaks` formatting in the front end without affecting the original text data?

In website content operation, we often encounter the need to display multi-line text input by users, such as article summaries, product descriptions, or comments.This text is usually stored in the database in plain text form, with the newline character (`\n`) as its only structural identifier.However, directly outputting plain text with line breaks to a web page often loses the original paragraph and line break effects, resulting in the content being displayed as a long, unordered block of text, which seriously affects the reading experience.Keep the original data pure, do not add any unnecessary HTML tags at the database level, which is an important principle of content management

2025-11-08

Does the `linebreaks` filter affect the display of images or links embedded in multiline text?

In AnQi CMS template development, the `linebreaks` filter is a commonly used tool, which is mainly used to convert the newline characters (\n) in multi-line text content into HTML paragraph tags (\u003cp\u003e) and line break tags (\u003cbr/\u003e), thereby better presenting the content on the web.Then, how will the `linebreaks` filter handle the embedding of images or links in multi-line text, and will it affect their normal display?This is a problem that many content operators and template developers are concerned about

2025-11-08

Does the `linebreaks` filter preserve these indents in my multiline text?

In AnQiCMS (AnQiCMS) template development, dealing with multi-line text is a common requirement, especially when the text contains specific formats, such as indentation.AnQi CMS provides the `linebreaks` filter to assist in handling these situations, but it has a specific mechanism for handling indentation that is worth exploring.The `linebreaks` filter is responsible for converting newline characters in text to HTML's `<p>` and `<br/>` tags to ensure that the text content is displayed correctly segmented on the web page.specifically

2025-11-08