How to truncate long text and display an ellipsis?

Calendar 👁️ 69

In the presentation of website content, we often need to refine some long text, such as displaying summaries on article list pages or showing brief descriptions on product cards.If the entire content is displayed directly, it may not only destroy the neat layout of the page, but may also affect the efficiency of users quickly browsing information.Therefore, truncating long text and adding an ellipsis at the end has become a common strategy to enhance user experience.AnQi CMS is an efficient content management system, built-in flexible template filters, which can help us easily achieve this function.

The template engine of Anqi CMS provides a series of powerful text processing filters, the core tool for truncating text and displaying an ellipsis istruncatecharsandtruncatewords.

truncatecharsThe filter, as the name implies, truncates according to the number of characters.It starts counting from the beginning of the text, stops at the specified number of characters, and automatically adds an ellipsis (...) at the truncation point.Whether it is Chinese, English, numbers, or various symbols, they are all considered as a character unit.This filter is particularly useful when precise control of text display length is required, for example, to maintain consistent content block height in card layouts.

AndtruncatewordsThe filter truncates at word boundaries. It identifies words in the text and counts them, truncating at the number of words you set, and also appending an ellipsis at the end.This approach is more friendly for websites mainly in English content, as it can avoid unnatural truncation in the middle of words, thus better maintaining the readability and semantic integrity of the text.

However, the text content we often handle is not always purely text; it may contain various HTML tags, such as bold, italic, links, or paragraph formatting.If the above plain text truncation filter is used directly, it is likely to truncate the HTML tags incompletely, which may lead to rendering errors or style confusion on the page.

To properly handle the truncation of rich text content, Anqi CMS further providestruncatechars_htmlandtruncatewords_htmlThese advanced filters. They are HTML-aware truncation tools that can intelligently check and close any incomplete HTML tags while truncating text, ensuring that the output HTML fragment remains valid and does not disrupt the page structure.After using these HTML-aware filters, in order for the browser to correctly parse and render the HTML content within, rather than displaying it as plain text, we also need to use it immediately after the template output,|safea filter. For example, you can write it like this:{{ archive.Content|truncatechars_html:150|safe }}.

These text truncation techniques are widely used in the daily operation of Anqi CMS websites. For example, on article or news list pages, we can usearchive.Description|truncatechars:120To display the abstract of each article, it can attract users to click and maintain the uniformity of the list.In the product display area, truncate the brief description of product details to effectively highlight selling points without occupying too much space.In addition, even for the introduction of categories or tags, or excessively long page titles, they can be appropriately truncated according to actual design requirements.

When choosing an appropriate truncation method, it is recommended to decide based on the characteristics of your content and design goals. If you have precise requirements for the display width of text, or if you are handling Chinese content, thentruncatechars(ortruncatechars_html)It is usually a better choice because it can finely control characters. If your content is mainly English and you want to maintain the integrity of words, thentruncatewords(ortruncatewords_html)Will provide a better reading experience. Always remember that whenever text content may contain HTML tags, it should be given priority to use filters with_htmlsuffix, and cooperate|safeto ensure correct rendering.

Integrating these filters into the Anqi CMS template is very intuitive. You just need to use the pipe character after the variable to be processed|Connect the corresponding filter and parameters. For example, to display the summary of a document:

<p class="article-summary">{{ article.Description|truncatechars:150 }}</p>

{# 如果是带有HTML格式的内容,并且希望保留格式 #}
<div class="product-brief">{{ product.Content|truncatechars_html:200|safe }}</div>

{# 针对英文标题,按单词截断 #}
<h2>{{ news.Title|truncatewords:8 }}</h2>

In this way, Anqi CMS allows content operators to flexibly control the presentation of the website's front-end content, effectively enhancing the visual appeal and user experience without changing the original content.

Frequently Asked Questions (FAQ)

  1. Why do I usetruncatechars_htmlTruncated rich text content, but the ellipsis is not displayed at the end?Answer:truncatechars_htmlandtruncatewords_htmlThe filter will automatically add an ellipsis after truncating the content. If your original content (the actual visible character count after parsing HTML tags) is less than or equal to the truncation length you set, then the filter will not perform the actual truncation operation, and therefore, the ellipsis will not be displayed.An ellipsis will only appear when the content is actually shortened.

  2. What is the reason why the original HTML tag code is displayed on the page instead of the rendered format effect when I truncate the text containing HTML tags?Answer: This is usually because the security template engine of Anqi CMS, for security reasons, defaults to escaping all output content to prevent cross-site scripting (XSS) attacks. When you usetruncatechars_htmlortruncatewords_htmlThe filter processes rich text and the output is still considered by the system to potentially contain unchecked HTML, and is therefore escaped.

Related articles

How to safely display HTML or JS code in a template without escaping?

In AnQi CMS template development and content operations, sometimes we need to directly display HTML code or run JavaScript scripts on the page, rather than have them parsed by the browser as plain text.This usually occurs when it is necessary to embed third-party statistical code, advertisement scripts, or when displaying code examples in article content.However, AnQi CMS, for security reasons, defaults to escaping the content output to templates to prevent security vulnerabilities such as cross-site scripting (XSS).To safely display HTML or JS code in a template without being escaped

2025-11-08

How to manage and display the homepage banner carousel?

The homepage banner carousel is the face of the website, which not only attracts the attention of visitors in the first place, but also an important carrier for conveying brand information and promoting core products or services.In AnQiCMS, managing and displaying the homepage banner carousel is a straightforward and flexible process, allowing you to easily create an eye-catching visual focus for your website. ### One, manage your Banner content in the background To manage the homepage Banner carousel, you first need to log in to the AnQiCMS backend management interface. Usually

2025-11-08

How to display language switch options on the front-end page and support hreflang attributes?

AnQiCMS as an efficient and flexible content management system provides a solid foundation for users in global content promotion, and its built-in multilingual support feature is the key to achieving this goal.Display language switch options correctly on your website and configure the `hreflang` attribute for search engines, which not only greatly enhances user experience but is also an important step for international SEO optimization.### One, Preparation: Enable AnQiCMS Multilingual Functionality AnQiCMS itself has strong multilingual support capabilities

2025-11-08

How to customize website structured data (JSON-LD) to improve search engine display?

It is crucial to make the website content stand out in the search engine results page (SERP) in today's highly competitive online environment.Structured data, especially JSON-LD format, is the key tool to achieve this goal.It helps search engines better understand the content of the page, thereby giving them the opportunity to display richer and more attractive "rich media summaries" (Rich Snippets), such as the publication date of the article, the author, ratings, product prices, etc., which can significantly increase click-through rates.

2025-11-08

How to format floating-point numbers, controlling the number of decimal places displayed?

Website content operation, it is crucial to display numbers accurately, especially floating-point numbers such as prices and statistics, for enhancing user trust and page professionalism.AnQi CMS knows this, providing simple and flexible tools in its powerful template engine, allowing content creators to easily control the display format of floating-point numbers, including decimal places, without delving into complex programming.Next, we will discuss how to use built-in filters in Anqi CMS templates to elegantly format floating-point numbers.### Use `floatformat`

2025-11-08

How to concatenate or replace strings before displaying them?

In AnQiCMS (AnQiCMS) content operation, we often encounter the need to dynamically process strings displayed on the front end of the website.To enhance the user reading experience, optimize search engine inclusion, or maintain consistency in content display, flexibly concatenating or replacing strings is a very practical skill.AnQi CMS, with its efficient template engine based on Go language, provides us with powerful and convenient tools, making these operations easy to implement at the template level.String concatenation

2025-11-08

How to remove extra spaces or characters from a string before displaying content?

During the operation of a website, we often encounter unnecessary spaces, line breaks, or special characters in the content. These 'impurities' not only affect the appearance of the content and the user's reading experience, but may also have a negative impact on the layout of the page and search engine optimization (SEO).AnQiCMS (AnQiCMS) is an efficient content management system that provides flexible and powerful template functions, among which the built-in template filters are the tools to solve such problems.Before you display content on the page, clean the string

2025-11-08

How to debug variables in a template and display their structure and values?

During the development and maintenance of website templates, we often encounter such situations: a page element does not display as expected, or data seems missing or incorrectly formatted.This is the key to quickly locate the problem, as you can clearly view the structure and specific values of variables in the template.For users who use AnQiCMS, the system provides powerful and convenient debugging tools to help us easily "penetrate" template data.Why do we need to debug template variables? Data is passed through variables in the AnQiCMS template.

2025-11-08