When it comes to generating abstracts based on article content, truncation length is a core consideration.There are two common ways to truncate: by character count and by word count.Truncating by character count is precise, but it often results in words being abruptly cut off, affecting the readability.While truncating by word count can better maintain semantic integrity, making the abstract more natural for readers.

For AnQiCMS users, the system is based on Django template engine syntax, providing strong flexibility. We can easily achieve the need to truncate the article summary by word count using built-in filters. This feature mainly depends ontruncatewordsfilter.

truncatewordsFilter: Smart extract summary by word

truncatewordsA filter specifically designed to truncate strings based on word count, it intelligently truncates at word boundaries to ensure that each truncated word is complete, greatly enhancing the reading experience of the summary. This is different from truncating by characters (such astruncatecharsThe filter may cause words to be cut off halfwaytruncatewordsMore emphasis on semantic coherence

How to usetruncatewordsFilter?

In the template files of AnQiCMS, for example, while you are editing an article list page, througharchiveListAfter obtaining article data, the label will usually traversearchiveseach element in the setitemto display the article information. The summary or abstract of the article is generally stored initem.Descriptionin the field.

to beitem.DescriptionTruncate to a specified number of words, just use it like this:

{{ item.Description|truncatewords:30 }}

In this example,30Indicate that you want the abstract to be truncated to the first 30 words. The system will automatically handle the truncation logic and add an ellipsis ("...") at the end of the content to make the abstract look more complete and professional.

Handle content that includes HTML tags:truncatewords_html

Sometimes, the summary content you have may not be plain text, but is directly taken from the articleitem.Contentpart, anditem.ContentMay contain various HTML tags (such as<strong>/<em>/<p>etc.). If a string containing HTML tags is used directlytruncatewords, it may damage the HTML structure, causing the page to display abnormally.

To deal with this situation, AnQiCMS provides another very practical filter:truncatewords_html. This filter is withtruncatewordsIt functions similarly, but it will intelligently identify and process HTML tags, maintaining tag integrity while extracting content to avoid display errors on the page.

When you need to extract an abstract that may contain HTML tags, you can use it like this:

{{ item.Content|truncatewords_html:50|safe }}

here,50It indicates that the first 50 words are to be extracted. It is especially important to note that, due totruncatewords_htmlThe result is still HTML, to ensure that the browser correctly parses and displays these tags, you need to add it at the end of the filter chain|safe.|safeThe filter tells the template engine that this content is safe and does not need to be automatically escaped, thus allowing HTML tags to be rendered normally.

Tip and **Practice:

  • Select the appropriate word count:
  • Preview and adjust:In practical applicationtruncatewordsortruncatewords_htmlMake sure to preview the effect on different devices (PC, mobile) to ensure that the abstract is presented beautifully and clearly on various screen sizes.
  • withtruncatecharsThe difference is:AlthoughtruncatecharsIt can also extract text, but it calculates by character count, which may break a word in the middle. Compared to,truncatewordsandtruncatewords_htmlPay more attention to the integrity of words, it is a better choice in scenarios where it is necessary to maintain the readability of the text.

Bytruncatewordsandtruncatewords_htmlThese flexible and powerful filters make AnQiCMS summary generation both intelligent and beautiful, helping you present content better and attract readers.


Frequently Asked Questions (FAQ)

  1. Question:truncatewordsIs an ellipsis automatically added after truncation? Can the style or content of the ellipsis be modified? Answer:Yes,truncatewordsandtruncatewords_html

  2. Question: If the abstract of my article is too short, it does not reachtruncatewordsWhat is the number of words set, will it also add an ellipsis? Answer:No.truncatewordsThe filter will only truncate content that exceeds the set word count and add an ellipsis. For example, if the original content isitem.DescriptionThe number of words in the phrase itself is less than the number of words you have set to extract (for example30), then the content will be output as is without adding an ellipsis to maintain the integrity of the content.

  3. Question:truncatewords_htmlDoes the filter retain all tags when processing HTML content? For example, like<div>such block-level tags? Answer: truncatewords_htmlThe filter intelligently tries to preserve the integrity of the HTML structure during the extraction process, which means it will close tags as much as possible to avoid page errors.But it does not retain all tags and their styles.Its main purpose is to prevent HTML syntax errors caused by truncation, rather than retaining the rendering effect of all original HTML.For complex HTML structures, it is recommended to directly edit the summary field in the background management interface, providing a clean and concise HTML snippet or plain text as a summary, so that the front-end display will be more controllable.