In the presentation of website content, we often need to condense long text information to meet different layout requirements, such as displaying article summaries on list pages, brief descriptions on product cards, etc.This not only makes the page look neater, but also helps visitors quickly grasp the core information.truncatewordsFilter is your powerful assistant for truncating text by word count.

Skillfully concise, making the content clear at a glance:truncatewordsFilter Parsing

truncatewordsThe filter, as its name suggests, is primarily designed to truncate text based on the number of words. When the number of words in the original text exceeds the threshold you set, it will intelligently cut off at the end of the last complete word and automatically add an ellipsis at the end of the text....)。The cleverness of this processing method lies in its respect for the integrity of words, avoiding the possibility of half words or unclear meanings that may arise from character truncation, thereby significantly enhancing the readability of the content.

How to usetruncatewords?

In the templates of Anqi CMS, usetruncatewordsThe filter is very intuitive. You just need to pass the text variable to be processed through the pipe symbol|to the filter, and specify the number of words you want to keep.

The basic syntax structure is as follows:

{{ text_variable|truncatewords:number }}

Here are thetext_variableIs the text you want to truncate (such as article description)item.Description), whilenumberis the number of words you want to retain.

Let's understand how it works with some specific examples:

Example 1: Truncating English text words

Suppose your text content is"This is a long test which will be cutted after some words."If you want to keep the first 9 words:

{{ "This is a long test which will be cutted after some words."|truncatewords:9 }}

The output will be:This is a long test which will be cutted after some ...

Example 2: Truncating Chinese text words

For Chinese, Japanese, Korean and other languages separated by non-space,truncatewordsThe behavior of the filter will be somewhat special.If the text does not contain spaces, the filter tends to treat the entire text as a 'word'.This means that it may not truncate in the middle of the text unless you specify the number of words as 0.

Suppose your text content is"你好世界":

{{ "你好世界"|truncatewords:1 }}

the output result is still:你好世界

This is because你好世界considered as a whole 'word'. But if the text contains spaces, such as"你好 世界":

{{ "你好 世界"|truncatewords:1 }}

The output result may be:你好 ...

When dealing with Chinese and other languages, if finer truncation control is needed, it may be necessary to combine the actual text structure or consider using other filters.

Actual application scenarios

truncatewordsFilter is widely used in the operation of website content:

  • Blog or news list page:When displaying the article list on the homepage or category page, the detailed content of each article may be very long, which may make the page look redundant. UsetruncatewordsCan easily truncate the article description into an abstract of 30, 50 words, which can attract clicks and keep the page tidy.
  • Product display page:On e-commerce websites, if the product features or brief introductions are too long, it may affect the user's browsing efficiency.Through this filter, you can uniformly truncate the product description to ensure that all cards or list items have consistent heights, enhancing the visual experience.
  • Content recommendation module:In the sidebar or bottom content recommendation area, space is usually limited.truncatewordsCan help you quickly generate refined recommendation titles or summaries, improving the click-through rate of recommended content.

WithtruncatecharsDifferences and Selection

The Anqi CMS also providestruncatecharsFilter, which truncates text by specifying the number of characters. When should you choose it?truncatewordsand when to choosetruncatechars?

  • truncatewords:Give priority to maintaining the semantic integrity of the text and the independence of words.It is more suitable for scenarios that require displaying complete words and avoiding word segmentation to cause reading difficulties.The disadvantage is that the final character length cannot be controlled accurately.
  • truncatechars:Consider precise control of the final character length. It is more suitable for scenarios with strict display space constraints, even if truncation may occur in the middle of a word.

In general, if you attach more importance to the readability and elegance of the content,truncatewordsit is a good choice. If there are strict requirements on the length of characters,truncatecharsthen it is more suitable.

Process content containing HTML tags

It is worth noting that if your text content contains HTML tags (such as text with bold, links generated by rich text editors), it is used directly.truncatewordsThe filter may destroy the structure of HTML tags, causing the page to display abnormally.

In this case, AnQi CMS provides a special solution:truncatewords_htmlFilter.truncatewords_htmlCan intelligently parse and truncate HTML content while ensuring that all tags are properly closed to maintain the normal rendering of the page. When the truncated content may contain formatting, it is essential to usetruncatewords_htmlTo ensure compatibility.

Summary

truncatewordsThe filter is a powerful and practical tool in the Anqy CMS template, which can help you present text content summaries in an elegant and readable manner, greatly enhancing the user experience and the aesthetic beauty of the webpage. By understanding its working principle, combined withtruncatecharsandtruncatewords_htmlThe characteristics can be flexibly applied in different scenarios to create more attractive websites.


Common Questions (FAQ)

1.truncatewordsandtruncatecharsWhat are the differences between filters? truncatewordsThe filter truncates based on the number of "words" in the text, it tries to preserve the integrity of words, and adds an ellipsis after the last complete word.truncatecharsThe filter truncates based on the number of “characters”, regardless of whether it is in the middle of a word, it will truncate directly. It is usually used in scenarios where there are strict pixel or layout limits on text length.

2. If the content I need to truncate contains HTML tags, which filter should I use?It is recommended to use for content that contains HTML tagstruncatewords_htmlFilter.It can intelligently parse HTML structure, ensuring the correct closure of tags while truncating text, to avoid page layout chaos or display errors due to truncation.truncatewordsIt may destroy the HTML structure.

3.truncatewordsHow does the filter handle languages such as Chinese that are not separated by spaces?For languages such as Chinese, Japanese, and Korean that do not have clear space-separated words,truncatewordsThe filter usually treats a continuous piece of text as a 'word'.This means that if the number of words you set is not zero, it may not truncate in the middle of the text, but display the entire text instead, unless the text contains spaces.truncatecharsor combine with other custom logic.