In website content operation, generating a concise and beautiful content summary (or called an introduction, guide) is a key link 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 text filters.Today, we will delve deep intowordwrapThe filter is suitable for generating content summaries and introducing better alternatives.

UnderstandingwordwrapThe principle of the filter.

wordwrapThe filter is a practical tool provided by the AnQiCMS template engine, which main function is to automatically wrap long text according to the specified length. When the length of a segment of text exceeds the specified value,wordwrapAttempts to insert line breaks between words to prevent text overflow in containers or layout confusion. For example,{{ content|wordwrap:20 }}Breaks the text into lines approximately every 20 characters.

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

wordwrapThe limitations of filters in content summaries

ThoughwordwrapExcellent in some scenarios but its limitations become apparent in content summarization, especially in the context of AnQiCMS widely used on Chinese websites:

  1. Chinese line break problem:The document of AnQiCMS clearly states,wordwrapThe filter is 'splitting words by spaces'. This means that for continuous Chinese text, as there is no natural space separation between Chinese characters,wordwrapThe filter cannot insert line breaks between them. Even if a smaller line break length is set, a long Chinese sentence may not be effectively wrapped, which may still lead to text overflow or poor display.This is a fatal defect for Chinese content summaries.
  2. Lacks truncation feature:One of the core requirements of content summary is "truncation" - that is, stopping the display of text after reaching a certain length, and usually ending with an ellipsis (...) indicating 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 if the original abstract is too long, even if it wraps, the user will still see the entire content, which is inconsistent with the purpose of the abstract being 'concise'.
  3. Insufficient length control:The summary content often needs to be precisely controlled for display length, for example, displaying the first 100 characters.wordwrapBy "approximately" some character length for line breaks, this makes it difficult to accurately control the final display length of the summary, especially when the actual number of lines and visual length after line breaks may vary under different screen sizes and font sizes.

An alternative that is more suitable for content summarization

Taking into accountwordwrapLimitations, AnQiCMS provides other more suitable filters for generating content summaries, especiallytruncatecharsandtruncatewords.

  1. truncatechars(Truncated by characters and followed by an ellipsis):This is the most recommended filter for generating content summaries, especially suitable for Chinese content.truncatecharsCan truncate text accurately according to the specified character count and automatically add an ellipsis after truncation...)。It will correctly handle Chinese, English, and other characters, whether single-byte or multi-byte, and will calculate and truncate according to the actual character count.
    • Example: {{ archive.Description|truncatechars:100 }}It will truncate the first 100 characters of the summary and add at the end....
  2. truncatewords(Truncated by word and added with an ellipsis):If your content summary is mainly in English and you want to truncate at the boundary of complete words,truncatewordsIt would be a good choice. It would truncate the text based on the specified word count and add an ellipsis. This ensures the natural fluidity of the English abstract and prevents words from being cut in half.
    • Example: {{ archive.Description|truncatewords:20 }}It will truncate the first 20 words of the summary and add at the end....
  3. truncatechars_htmlandtruncatewords_html(Truncating HTML content):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 truncate text while trying to maintain the integrity of the HTML structure, to avoid rendering issues caused by unclosed tags.

Summary suggestions

When generating content abstracts in AnQiCMS, we should choose the most suitable tool based on actual needs.wordwrapThe filter is valuable in scenarios where it is necessary to perform flexible line breaks on long text, without concerning about precise truncation length, and mainly in English, such as displaying a whole paragraph 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 the feature of truncating and adding an ellipsis is more in line with the general understanding and expectations of users for summaries.


Frequently Asked Questions (FAQ)

1. WhywordwrapFilter cannot correctly implement line breaks in Chinese content summaries? wordwrapThe filter is mainly determined by recognizing spaces between words to determine the line break point.Because Chinese characters are usually not separated by spaces, it cannot determine the "word" boundaries, so for continuous Chinese text, even if the line length is set, it cannot insert line breaks between the characters, so it cannot achieve the expected line break effect.

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 usetruncatecharsortruncatewordsFilter. For example,{{ archive.Description|truncatechars:100 }}An ellipsis will be automatically added after truncating the first 100 characters.

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