In website content operation, generating a concise and beautiful content abstract (or what is called a synopsis or guide) is a key step to improve user experience and SEO performance.A good summary can quickly attract the reader's attention and also help search engines better understand the content topic.In AnQiCMS (AnQi CMS), we have various tools to handle text, including the text filter.wordwrapFilter whether it is suitable for generating content summaries and introducing better alternative solutions.

UnderstandingwordwrapThe working principle of the filter

wordwrapThe filter is a practical tool provided by AnQiCMS template engine, which mainly functions to automatically wrap long text according to the set length. When the length of a text exceeds the specified value,wordwrapIt will try to insert line breaks between words to prevent text overflow in the container or to avoid layout confusion. For example,{{ content|wordwrap:20 }}It will wrap the text every 20 characters or so.

This filter is very effective in processing English text because it identifies spaces between words as line breaks.This can effectively maintain the neatness and readability of long continuous English content, avoiding the situation where excessively long words 'burst' the layout in limited space.

wordwrapThe limitations of the filter in content abstracts

AlthoughwordwrapOutstanding in some scenarios, but its limitations become apparent in the generation of content summaries, especially under the background of AnQiCMS widely used in Chinese websites:

  1. English line break problem:AnQiCMS documentation explicitly states,wordwrapThe filter is 'separated by spaces' for words. This means that for continuous Chinese text, since there are no natural spaces between Chinese characters,wordwrapThe filter will not be able to insert line breaks between them.Even if a smaller line break length is set, a long sentence in pure Chinese may not be effectively line broken, which may still lead to text overflow or poor display.This is a fatal defect for the Chinese content summary.
  2. Lacks truncation function:The core requirement of the content summary is one of "truncation"—that is, to stop displaying text after reaching a certain length, and usually end with an ellipsis (...) to indicate that the content has not ended.wordwrapThe filter is only responsible for line breaks, it does not automatically truncate text, nor does it add an ellipsis at the end of the text.This means that even if the original abstract is too long and wraps, the user will still see all the content, which is inconsistent with the purpose of the abstract to be concise.
  3. Insufficient precise length control:Content abstract usually requires precise control of display length, for example, displaying the first 100 characters.wordwrapThrough the line break based on the "approximate" character length, it is difficult to achieve precise control over the final display length of the summary, especially under different screen sizes and font sizes, where the actual number of lines and visual length after line break may change.

A more suitable alternative for content abstracts

Taking into considerationwordwrapLimitations, AnQiCMS provides other more suitable filters to generate content abstracts, especiallytruncatecharsandtruncatewords.

  1. truncatecharsThis is a truncation and ellipsis added by character:This is the most recommended filter for generating content summaries, especially suitable for Chinese content.truncatecharsCan truncate text accurately according to the specified number of characters and automatically add an ellipsis (...)。It will correctly handle Chinese, English, and other characters, whether single-byte or multi-byte characters, and will calculate and truncate according to the actual number of characters.
    • Example: {{ archive.Description|truncatechars:100 }}Extracts the first 100 characters of the abstract and appends....
  2. truncatewords(Truncated by words and followed by an ellipsis):If your content summary is mainly in English and you want to truncate at the boundary of a complete word,truncatewordsThis would be a great choice. It will truncate the text based on the specified number of words and add an ellipsis. This ensures the natural flow of the English summary and avoids cutting words in half.
    • Example: {{ archive.Description|truncatewords:20 }}Extracts the first 20 words of the summary and appends....
  3. truncatechars_htmlandtruncatewords_html(Truncation of HTML content processing):In rare cases, if your content summary may contain some HTML tags (although it is usually not recommended), AnQiCMS also providestruncatechars_htmlandtruncatewords_htmlThese two filters strive to maintain the integrity of the HTML structure while truncating text, to avoid page rendering issues caused by unclosed tags.

Summary suggestions

In AnQiCMS, when generating content summaries, we should choose the most suitable tool according to actual needs.wordwrapThe filter has its value in scenarios where flexible line breaks are needed for long texts, without concerning the exact truncation length and mainly in English, such as displaying the entire description in a flexible layout text block.

However, for most content summarization scenarios, especially when the summary needs to be precisely controlled in length, contain Chinese, and automatically add ellipses,truncatecharsThe filter is undoubtedly the better choice.It can provide more precise character control, and its feature of adding an ellipsis after truncation is more in line with the general perception and expectation of users for summaries.


Common Questions and Answers (FAQ)

1. WhywordwrapFilter cannot correctly implement line breaks in Chinese content summaries? wordwrap

2. How can I make the content summary automatically display “…” after truncation? wordwrapThe filter itself does not provide the functionality to automatically add ellipses. To achieve this effect, you should usetruncatecharsortruncatewordsa filter. For example,{{ archive.Description|truncatechars:100 }}An ellipsis will be automatically added after the first 100 characters.

3. If my content summary may contain some HTML tags, such as bold or links, which filter should I use for truncation?In this case, to truncate while avoiding the destruction of the HTML structure, you should usetruncatechars_htmlortruncatewords_htmlFilter. They will intelligently handle the closing of HTML tags when truncating, ensuring the correct rendering of the page.