What is the essential difference between the `truncatechars` and `truncatewords` filters, and how should one choose?

Calendar 👁️ 107

In Anqi CMS template development, in order to better display content summaries or control page layouts, we often need to truncate text. At this point,truncatecharsandtruncatewordsThese two filters come into play. They can both help shorten long text and add an ellipsis at the end, but there is a fundamental difference in their truncation logic. Understanding these differences is crucial for selecting and using them correctly.

truncatechars: truncate to the character level

truncatecharsA filter, as the name implies, truncates according to the number of characters. When you set a numeric parameter for it, it counts from the beginning of the text until it reaches the specified number of characters, then truncates the text at that position and adds an ellipsis at the end (...Please note that this number parameter isincludeThe number of characters in the ellipsis.

Core characteristics:

  1. Character counting is accurate:It does not care about the integrity of the word, every letter, number, symbol, and even a space is regarded as a character. For Chinese characters, it also counts them as a character unit.
  2. The word may be truncated:Since it is truncated strictly according to the number of characters,truncatecharsIt may be truncated in the middle of a word, resulting in a slightly rigid display effect that does not conform to the natural reading habit.
  3. HTML processing:When processing rich text content containing HTML tags, use directlytruncatecharsIt will destroy the HTML structure, causing the page to display abnormally. At this time, it should use its HTML-friendly versiontruncatechars_html.truncatechars_htmlIt will intelligently close unclosed HTML tags at truncation to ensure the integrity of the page structure.

Example:

Suppose there is a string"AnQiCMS是一个功能强大的CMS系统。" {{ "AnQiCMS是一个功能强大的CMS系统。"|truncatechars:10 }}It may outputAnQiCMS是....

And{{ "AnQiCMS是一个功能强大的CMS系统。"|truncatechars:15 }}Possible outputAnQiCMS是一个功....

If the content is HTML code, for example<p><strong>AnQiCMS</strong> 是一个内容管理系统。</p>, using{{ value|truncatechars_html:20 }}It will intelligently truncate and properly close tags, such as<p><strong>AnQiCMS</strong> 是一个内容...</p>.

truncatewordsTruncation by word units:

withtruncatecharsdifferent,truncatewordsThe filter is based on word truncation. It will break down the text content into individual words (usually separated by spaces), and then truncate according to the specified number of words and add an ellipsis at the end.

Core characteristics:

  1. Keep the word intact: truncatewordsIt ensures that each word is complete without any omissions. It does not truncate words in the middle, thus ensuring the readability and naturalness of the text.
  2. Not suitable for languages without spaces:This istruncatewordsThe most significant limitation when handling Chinese, Japanese, Korean (CJK) languages in Anqi CMS (or other template engines based on Western language logic). Since CJK languages do not have natural word spacing between words,truncatewordsIt often treats a continuous paragraph of Chinese text as a single "super long" word.This means that if you specify truncating 10 words, and your Chinese paragraph does not have spaces, it is very likely to return the entire paragraph because "10 words" to it is just one "super long word".
  3. HTML processing:Similarly, when dealing with rich text content containing HTML tags, it should usetruncatewords_htmlversion. It can balance between maintaining word integrity and HTML structure integrity.

Example:

Suppose there is a string"AnQiCMS is a powerful Content Management System." {{ "AnQiCMS is a powerful Content Management System."|truncatewords:5 }}It will output.AnQiCMS is a powerful Content....

But if the string is Chinese"安企CMS是一个功能强大的内容管理系统。",{{ "安企CMS是一个功能强大的内容管理系统。"|truncatewords:5 }}It is likely to output安企CMS是一个功能强大的内容管理系统。(i.e., not truncated), because it treats the whole sentence as a single word.

If the content is HTML code, for example<p><strong>AnQiCMS</strong> is a Content Management System.</p>, using{{ value|truncatewords_html:5 }}It will intelligently truncate and properly close tags, such as<p><strong>AnQiCMS</strong> is a Content Management...</p>.

Essential difference and selection suggestions

Features/Filter truncatechars/truncatechars_html truncatewords/truncatewords_html
Truncated Unit Character (including spaces, punctuation, Chinese characters) Word (separated by spaces)
Word Integrity Not guaranteed, may be truncated in the middle of a word Ensure, always keep the word complete
Exact length High, precise control of the total number of characters (including ellipses) Low, depending on the word length, the actual number of characters is not accurate
Chinese supported Good, counts characters, truncation is predictable Poor, may treat a segment of Chinese as a word, resulting in no truncation
HTML supported truncatechars_htmlGuarantees structure truncatewords_htmlGuarantees structure

How to choose?

  • When you need to strictly control the total length of displayed characters (for example, in table cells, or to ensure SEO meta descriptions are precise to the character count, and you do not mind words being truncated), and your content may contain Chinese, Japanese, Korean, and other languages without space separation, please prioritize choosingtruncatechars.For Chinese string,truncatecharsit can provide a more intuitive and predictable truncation effect.
  • When you are more concerned with the readability and semantic integrity of text, and do not want words to be truncated, and your content is mainly English or other languages separated by spaces, please prioritize the choicetruncatewords.It can provide users with a smoother reading experience, for example, in the summary of the article list.
  • If you choose any truncation method, if your content is rich text (including HTML tags), please make sure to use the corresponding_htmlversion (i.e.truncatechars_htmlortruncatewords_html)This will prevent the page HTML structure from being damaged due to truncation.

In summary, understanding the underlying logic and the advantages and disadvantages of these two filters, especially their performance in handling different languages, can help you make wise choices in the content operation of Anqi CMS, thereby improving the display quality and user experience of the website content.


Frequently Asked Questions (FAQ)

  1. Does the numeric parameter in the filter contain the length of the ellipsis (…)?Yes,truncatecharsandtruncatewordsAll numeric parameters set by the filterincludeThe ellipsis (}...The character length of. For example, if you settruncatechars:10, then the final text (including the ellipsis) will be at most 10 characters long.

  2. If my content is in HTML format, use it directlytruncatecharsortruncatewordsWhat problems might there be?directlytruncatecharsortruncatewordsProcessing HTML content may destroy the structure of HTML tags. For example, a<strong>The tag may be truncated, but its closing tag</strong>was omitted, causing the page to display chaos. Therefore, when processing HTML content, be sure to use its corresponding HTML-friendly version:truncatechars_htmlandtruncatewords_htmlThey will intelligently close incomplete HTML tags.

  3. The content of my website is mainly Chinese, which filter should be prioritized to truncate the article summary?For websites mainly containing Chinese content, it is usually recommended to usetruncatecharsortruncatechars_html. This is because Chinese words are not separated by spaces.truncatewordsThe filter treats a continuous block of Chinese text as a single 'big word', which may cause it not to truncate or unpredictable truncation behavior even if you set a smaller word count. AndtruncatecharsIt will truncate according to the actual character count (each Chinese character counts as one character), resulting in a more expected outcome.

Related articles

How to truncate the title of the article and automatically add an ellipsis in the AnQiCMS template?

In AnQi CMS template design, in order to ensure the beauty of the page and the uniformity of the layout, we often need to truncate the article title and automatically add an ellipsis after truncation.AnQiCMS provides a simple and efficient template filter to meet this requirement, making content display more flexible.

2025-11-07

How to use `split` and `join` filters to standardize user tag inputs?

In the daily content operation of AnQi CMS, the tags (Tag) submitted by users often face a common problem: the format is not unified.Some users are accustomed to separating with commas, some with semicolons, and even possibly with Chinese commas or even just spaces.These irregular inputs, if directly displayed on the website front-end, not only affect the aesthetics but may also reduce the usability of tags, for example, causing trouble when generating tag clouds or performing SEO optimization.

2025-11-07

How to combine a dynamically generated array (such as a tag list) into a string with a specified delimiter for display in a template?

In website content operation, we often need to display a series of related information in the form of a list to users, such as all tags of an article, multiple characteristics of a product, or a set of image URLs.AnQiCMS's template system usually provides these data in the form of a dynamic array (or sometimes called slices, lists).

2025-11-07

What are the main differences and application scenarios between the `make_list` filter and the `split` filter when splitting strings into arrays?

In AnQi CMS template development, it is often encountered that a string needs to be split into multiple parts for further processing or dynamic display.To meet this requirement, AnQiCMS template engine provides two very useful filters, `make_list` and `split`.Although they can both convert strings to arrays, there are obvious differences in their core functions, splitting logic, and applicable scenarios in actual use.Understanding these differences can help us handle content more efficiently and accurately.

2025-11-07

How to safely truncate text to a specified length for articles containing HTML tags using `truncatechars_html`?

When using AnQiCMS to manage website content, we often encounter such a scenario: to make the article content more beautiful in layout, various HTML tags are used to enrich the visual effects, such as paragraphs (`<p>`), images (`<img>`), links (`<a>`), bold (`<strong>`), and so on.However, on the article list page or in the related recommendation module, we often need to display the abstracts or parts of these articles, but we cannot directly display the entire long discourse. At this time

2025-11-07

Can the `truncatewords_html` filter correctly handle nested HTML tags while truncating, preventing page layout chaos?

In website operations, we often need to summarize long content to attract users to click and view the details.However, directly truncating rich text content containing HTML tags often leads to page structure chaos, for example, a `<p><b>important information</p>` truncated to `<p><b>important` such a missing tag, not only ruins the visual beauty of the page, but may also lead to the entire page layout disorder.This is undoubtedly one of the most headache issues for content operators. AnQi CMS understands this pain point.

2025-11-07

How to ensure that all unclosed HTML tags are properly closed after truncating `truncatechars_html`?

In website content operation, we often need to extract a part of the long text such as articles and product descriptions as a summary, used for list display or card preview.This can not only effectively save page space, but also attract users' attention and guide them to click to view the full content.However, when these long texts contain HTML tags, simple character truncation often leads to unclosed tags, thereby destroying the page layout and affecting the user experience.AnQiCMS (AnQiCMS) understands this pain point and has built the `truncatechars_html` filter into the template engine

2025-11-07

How to batch remove all specific interfering characters or HTML entities from AnQiCMS article content?

It is crucial to maintain the purity and readability of content in website content operation.Whether it is content imported from outside, text processed by collection tools, or redundant characters inadvertently introduced during daily editing, these interfering factors may affect user experience and search engine optimization effects.AnQiCMS provides an efficient and powerful built-in tool to help you batch clean up various interfering characters or HTML entities from the article content, ensuring that your website content always maintains a high-quality state.### The necessity of understanding content cleaning As the accumulation of website content continues

2025-11-07