In AnQi CMS, in order to display the article summary on the list page or preview area, we often need to截取 article content. At this time,truncatewordsandtruncatecharsThese filters come into play.They can all help us condense long content, but there are significant differences in the truncation logic between them, especially when dealing with Chinese and English characters and words, their performance is even more different.Understanding these differences can help us better control the presentation effect of the summary.
truncatechars: precise character-based extraction of the summary
truncatecharsThe principle of the filter's operation is relatively straightforward: it starts counting characters from the beginning of the content one by one until it reaches the specified length. Once the set length is reached, the remaining content is truncated and an ellipsis is automatically added at the end (...)
Whether it is English characters, numbers, or Chinese characters,truncatecharsthey are all counted as independent characters. This means that if you set to extract 10 characters:
- For English content, it may be truncated in the middle of a word, for example, after extracting 10 characters from “AnQiCMS is powerful”, it may become “AnQiCMS i…”.
- For Chinese content, it accurately extracts the first 10 Chinese characters, for example, “AnQi CMS is a high-efficient and powerful content management system” is extracted to “AnQi CMS is a high-efficient and powerful content management…”.
Therefore, if you have a strict character count limit for the summary and do not mind content being cut off in the middle of a word, thentruncatecharsis an accurate and reliable choice.
truncatewords: Extract the summary by word logic
withtruncatecharsdifferent,truncatewordsThe filter focuses on "words" rather than individual characters.It will identify words based on spaces in the content and count the number of words.When the specified number of words is reached, the content will be truncated, and an ellipsis will be added at the end.
This logic of cutting words by words is very practical when processing English content.Because it ensures that the extracted summary is always composed of complete words, avoiding the appearance of half-words due to truncation.For example, after extracting 5 words from “AnQiCMS is a powerful and flexible system”, we get “AnQiCMS is a powerful and…”.}]
However, when processing Chinese content,truncatewordsThe performance does not seem ideal. Since the Chinese sentence structure usually does not have the "words" separated by spaces like English,truncatewordsThe filter tends to treat a continuous segment of Chinese text as a 'large word'. This means that if you try to usetruncatewords
to take into account the HTML structure extraction:_htmlVariant
It is noteworthy that AnQi CMS also providestruncatechars_htmlandtruncatewords_htmlThese special filters. It is strongly recommended to use the one with HTML tags when your article abstract may contain elements such as images, links, bold text, etc._htmlThe variant suffix.
These_htmlThe filter intelligently handles HTML tags while extracting characters or words, ensuring that the extracted content still maintains a valid HTML structure and avoids displaying errors due to unclosed tags. Imagine if your summary was cut off in the middle of a sentence.<p>tags or<a>Tags in the middle, and_htmlVariants will help you close tags correctly, ensuring the correct rendering of the page.
Actual application scenarios and selection suggestions
When choosing to usetruncatecharsOrtruncatewordsAt that time, we need to consider the following aspects:
- Content language:
- If your website mainly publishesEnglish contentand hope that the abstract can maintain the integrity of the words,
truncatewordsIs a better choice. If you pay more attention to the control of the total length of characters,truncatecharsit is more accurate. - If your website mainly publishesChinese contentthen
truncatecharsusually provides a more expected truncation effect. Due to the characteristics of the Chinese language,truncatewordsThe logic for truncating Chinese content may not work according to our 'Chinese word' concept.
- If your website mainly publishesEnglish contentand hope that the abstract can maintain the integrity of the words,
- Truncation accuracy requirement:
- When it is necessary to strictly control the number of characters, select
truncatechars. - Select when the word integrity needs to be maintained (limited to English)
truncatewords.
- When it is necessary to strictly control the number of characters, select
- Does the content contain HTML?:
- If the abstract is extracted directly from a rich text editor, it is likely to contain HTML tags. To avoid destroying the page layout of the abstract, it is essential to choose
truncatechars_htmlortruncatewords_html.
- If the abstract is extracted directly from a rich text editor, it is likely to contain HTML tags. To avoid destroying the page layout of the abstract, it is essential to choose
By understanding these differences, we can be more flexible and accurate in extracting article summaries in Anqi CMS, improving the user reading experience, and ensuring the good presentation of page content.
Frequently Asked Questions (FAQ)
Q1:truncatewordsIs there an alternative solution when the filter does not perform well in truncating Chinese content?A1: Yes, whentruncatewordsWhen the ideal word segmentation effect cannot be achieved for Chinese content, the most direct and effective alternative is to usetruncatecharsfilter.truncatecharsIt will cut according to the number of Chinese characters you specify, although it may not be able to split according to the "word" boundary of Chinese intelligently, but it can ensure that the length of the summary meets the expectation.If you want a more intelligent Chinese word segmentation summary, you may need to integrate a more complex Chinese word segmentation library on the backend, but this usually goes beyond the scope of the template filter.
Q2: How to determine if there is more content in the extracted article summary so that the 'Read More' link can be displayed?A2: In the template, you usually need to get the full content (or original length) of the article and the extracted abstract content, and then compare their lengths.If the length of the extracted summary is less than the length of the full content, it means there is more content available, and a "Read More" link can be displayed.{{ article.Description|length }}(The length of the original introduction) and{{ article.Content|truncatechars:100|length }}(The length of the extracted summary), or directly compare the original content with the extracted content for equality.
Q3: Will using these summary extraction filters affect SEO?A3: Using the summary truncation filter reasonably has a positive impact on SEO.The abstract can provide a brief overview of the page content for search engines, which helps search engines understand the theme of the page.Must be used.truncatechars_htmlortruncatewords_htmlHandle it to avoid breaking the HTML structureA tidy, semantically correct page is always more beneficial for SEO. At the same time, ensure that the full article content can be indexed by search engines on the detail page.