How to truncate long text content in a template and add an ellipsis for display?

Calendar 👁️ 70

In website content operation, we often encounter scenarios where it is necessary to display long text, such as article summaries, product descriptions, or user comments.If the complete content is displayed directly, it may cause the page to be long and disorganized, affecting the user's reading experience.At this time, the long text is intelligently truncated and an ellipsis (...) is added at the end, which becomes an elegant and practical solution.AnQi CMS is a powerful content management system that provides various convenient ways to meet this need with its flexible template engine.

The core tool for efficiently truncating long text content: Template Filter

The Anqi CMS template system follows the design philosophy of Django, using 'filters' to process variables and achieve various content formatting.For text truncation, the system has several very practical filters that allow us to easily control the display length of text.

1. Cut according to the number of characters:truncatechars

The most commonly used and most direct way to cut text istruncatecharsA filter that truncates text to a specified character count and automatically adds an ellipsis to the excess. It is important to note that this character count includes the ellipsis itself.

For example, if you want to display an 80-character summary of each article on the article list page, you can use it like this:

{{ item.Description|truncatechars:80 }}

Hereitem.DescriptionThis is the long text content you want to extract,80Then it is the maximum number of characters you want to display. If the original description exceeds 80 characters, it will be truncated at the 77th character and three dots will be added to form an ellipsis.If the original text is less than 80 characters, it will be displayed in full without an ellipsis.This filter is suitable for precise length control of plain text content.

2. Extract by word count:truncatewords

If your content places more emphasis on the integrity of words rather than the mere length of characters,truncatewordsThe filter would be a better choice. It would truncate based on the number of words, ensuring that each displayed word is complete, thereby improving the readability.

The method of use andtruncatecharsSimilar, the numbers at the end represent the number of words:

{{ item.Description|truncatewords:20 }}

This filter is especially convenient for processing English content, as it avoids the issue of half words caused by character truncation, maintaining clear semantics.In multilingual websites, it can provide a more friendly truncation effect for languages separated by spaces.

3. Smart handling of HTML content extraction:truncatechars_htmlandtruncatewords_html

Sometimes, the text content we extract may contain HTML tags, such as bold, italic, or links. Use them directly.truncatecharsortruncatewordsMay destroy these HTML structures, causing abnormal page display, such as unclosed tags or style chaos. Anqi CMS providestruncatechars_htmlandtruncatewords_htmlthese special filters.

The magic of it lies in the fact that, while extracting text, it intelligently retains and closes all unclosed HTML tags, ensuring that the output HTML code is still valid and structurally complete. This is crucial for handling rich text editor-generated content (such as theContentField),and it is very important when displayed on the list page or in the summary area.

The usage is the same as the previous two, but it should be noted that since the output is HTML content, it usually needs to be配合|safeFilter used to prevent the template engine from automatically escaping HTML tags:

{{ item.Content|truncatechars_html:150|safe }}

Or

{{ item.Content|truncatewords_html:50|safe }}

By_htmlFilter, you can safely extract rich text content without worrying about破坏 the page layout and functionality.

How to choose the appropriate cutting method?

In practical applications, the choice of filter depends on your specific needs and content characteristics:

  • For pure text titles and short sentences, aiming for fixed length, and not concerning the integrity of words, usingtruncatecharsFor example, the title summary of the news list.
  • For an English abstract, pursue the completeness of words, ensure clarity of meaning, usingtruncatewordsFor example, the brief introduction of a blog article.
  • For rich text content containing HTML tags (such as the beginning part of an article body)To avoid destroying the page structure, please be sure to usetruncatechars_htmlortruncatewords_htmland combining|safeFilter output.

Practical suggestions and **practice

These extraction methods are widely used in the template design of Anqi CMS.You can flexibly use it in areas such as the article list page, product display page, category page, etc. where a brief preview of the content is required.It is recommended to set different truncation lengths when designing, based on different display areas and device types (such as PC or mobile end), to achieve **user experience.For example, you may wish to truncate the number of characters (such as 40) on the mobile list page, while allowing more leeway (such as 100) on the PC end.Maintain consistent truncation length, which can also make the page look cleaner.

Bytruncatechars/truncatewordsAnd its HTML version filter, Anqi CMS provides powerful text processing capabilities for website operators, making the display of long text content both beautiful and practical.Reasonably utilizing these features will greatly enhance the user experience and content presentation quality of your website.


Frequently Asked Questions (FAQ)

Q1:truncatecharsandtruncatewordsDoes the truncation length include an ellipsis?A: Yes,truncatecharsWhen calculating the truncation length,Is includedthree ellipses (…) are automatically added at the end. For example, if you set the truncation

Related articles

How to define a temporary variable in a template and use it to display content?

In Anqi CMS, when managing website content, we often find that using temporary variables flexibly in templates can make the code more concise, efficient, easy to understand, and maintainable.The Anqi CMS template system has borrowed the syntax of the Django template engine, which provides a straightforward way to define and use these temporary variables. ### Temporary variables make the template more concise and efficient Why do we need to define temporary variables in the template?

2025-11-09

How to display the content, title, and image of a single page in a custom page?

In website operation, we often need to create unique display methods for specific content, such as designing a unique landing page for product features, service introductions, or company profiles.AnQiCMS (AnQiCMS) with its flexible content model and highly customizable features, makes everything very simple.Today, let's talk about how to easily display the content, title, and images of a custom page, making your website content more personalized.

2025-11-09

How to use breadcrumb navigation tags to display the current page path at the top of the page?

In website operation, a clear navigation path is crucial for user experience and search engine optimization.When a user is browsing deep pages on a website, a straightforward 'You are here' prompt can help them quickly locate and easily return to the parent directory.This is a kind of auxiliary navigation method, which we call breadcrumb navigation.AnQiCMS (AnQiCMS) is built-in with powerful breadcrumb navigation tags, allowing developers and content operators to easily display the current location path at the top of the page.What is breadcrumb navigation and why is it important?

2025-11-09

How to display a list of friendship links on the front page of the website?

In website operation, friendship links have always been an important means to enhance website authority, increase external traffic, and enhance user trust.If you are using AnQiCMS to manage your website and want to display a list of friend links on the frontend page, you will find that this process is surprisingly simple and efficient.AnQi CMS, with its simple design and powerful features, makes content management easy, and the display of friendship links is no exception.Next, let's take a look at how to display friend links in Anqi CMS.

2025-11-09

How to ensure that the template file uses UTF-8 encoding to avoid garbled Chinese content?

In website operation, the clear and accurate presentation of content is crucial.Especially when your website contains Chinese content, if you encounter a "garbled code" problem, it not only seriously affects the user experience, but may also damage the professional image of your website.For users of Anqi CMS, ensuring that all template files use the correct encoding, especially UTF-8, is the first step to avoid such problems and is also the most fundamental and critical guarantee.### Why is UTF-8 so important? UTF-8 is the full name of Unicode Transformation

2025-11-09

How to display a custom shutdown notice to visitors when the website is under maintenance?

As website operators, we always encounter situations where we need to maintain and upgrade the website, whether it is database optimization, system updates, or content adjustments. These operations sometimes require temporarily shutting down the website to avoid unexpected errors or incomplete content display during maintenance.However, simply displaying a "cannot open" error page on a website undoubtedly gives visitors a bad experience, and may even make search engines mistakenly believe that the website is permanently offline.AnQiCMS (AnQiCMS) fully understands this pain point and provides a graceful and flexible solution that allows you to maintain your website while it is closed for maintenance

2025-11-09

How to dynamically obtain and display the current year in the template?

In website operation, it is crucial to maintain the timeliness and accuracy of content, especially information like copyright year, which, if manually updated, is not only cumbersome but also prone to omission.Our company AnQi CMS provides a very convenient way to dynamically obtain and display the current year in templates, ensuring the latest information on the website.

2025-11-09

How to convert multi-line text content into HTML format using a newline character?

In content management, we often encounter situations where we need to enter multiline text, such as product details, service introductions, company profiles, or contact information.This content is typically segmented by pressing the Enter key during backend editing.However, when this content is directly presented on the website front end, you may find that it is 'squeezed' together, with all line breaks disappearing, which not only affects the reading experience but also makes the page look disorganized.AnQi CMS as an efficient enterprise-level content management system, of course, has considered this point and provided a variety of elegant solutions

2025-11-09