In managing content in AnQi CMS, the display method of the article summary (also commonly referred to as an abstract) is crucial for the overall aesthetics and user experience of the website.A good introduction can attract readers and help search engines understand the content.The default setting of Anqi CMS is to automatically extract the first 150 characters of the article content as a summary if the user does not manually fill in the introduction.This mechanism is convenient, but in some cases, we may prefer to control the length of the summary based on the number of words rather than characters, especially when dealing with multilingual content or when aiming to maintain a more natural reading experience.

Understanding the default summary handling method of AnQiCMS

When you edit articles in the Safe CMS backend, you will see a field called "Document Summary".If you manually enter content here, it will be displayed as the article's introduction.But if you choose to leave it blank, the system will intelligently extract it from the article content, and default to extracting the first 150 characters to fill in.Here, the word '字' usually refers to a character; for Chinese content, one Chinese character is considered one character; for English content, one letter or symbol is also considered one character.

This method of truncation based on character count can meet the needs in many cases.However, it may sometimes lead to results that are not up to expectations.For English content, 150 characters may contain many words, and sometimes we only want to keep it concise to about 20 words; conversely, for some technical articles, 150 characters may not be enough to express the core meaning, or it may truncate a word when cut, affecting the continuity of reading.Therefore, limiting the display of introductions by word count will greatly enhance the flexibility and professionalism of content presentation.

Implement the core method of limiting the number of words: using template filters

The Auto CMS provides a powerful and flexible template mechanism, allowing us to finely control the display of content by modifying the front-end template files. Although there is no direct option to set the number of words displayed in the introduction on the backend, we can achieve this by utilizing the template.FiltersTo achieve this goal.

In the template system of AnQi CMS, there are several very practical filters that can help us process text truncation, among which the one most suitable for controlling the summary by word count istruncatewordsandtruncatewords_html.

  • truncatewordsFilterThis filter is used to extract plain text content.It will truncate according to the number of words you specify and automatically add “…” at the end to indicate omission.It is suitable for your article summary where the expectation is plain text without any HTML tags.
  • truncatewords_htmlFilterIf your article summary may contain HTML tags (such as bold, links, etc.), and you want to keep these HTML structures after truncation, then you should usetruncatewords_html.This filter will cut text while trying to ensure the integrity of HTML tags, to avoid abnormal display of the page due to truncation of tags.|safeFilter, tells the template engine that this content is safe HTML and does not need additional escaping.

Operation steps with code examples

Now, let's see how to apply these filters in actual template files.

第一步:确定要修改的模板文件

The article summary will usually appear in multiple locations on the website, such as the article list on the homepage, the article list on the category page, and the search results page.You need to find the corresponding template file for displaying the article summary according to your website layout./template/你的主题名/),文件名可能包括index.html(首页)、archive/list.html(文章列表页) 或者其他自定义的列表模板。

第二步:定位文章简介的调用位置

在找到的模板文件中,你需要找出用来显示文章简介的代码。通常,这会是一个类似{{ item.Description }}or{{ archive.Description }}The variable call. Hereitemorarchiverepresents the current article object, whileDescriptionthe field is the brief introduction of the article.

Step 3: Applytruncatewordsortruncatewords_htmlFilter

If you want the article summary to display 20 words, you can modify the code like this:

  • If the summary is plain text or you do not need to retain the HTML tags:

    {# 原始调用,可能显示过长的字符简介 #}
    <p>{{ item.Description }}</p>
    
    
    {# 修改后:限制显示20个单词 #}
    <p>{{ item.Description|truncatewords:20 }}</p>
    
  • If the summary may contain HTML tags and you wish to preserve these tags:

    {# 原始调用,可能显示过长的字符简介 #}
    <div>{{ item.Description|safe }}</div>
    
    
    {# 修改后:限制显示20个单词,并保留HTML结构 #}
    <div>{{ item.Description|truncatewords_html:20|safe }}</div>
    

    Please note,truncatewords_htmlAfter the filter is processed, it is usually necessary to use chaining.|safeFilter to ensure that HTML content is parsed correctly instead of being escaped.

Step 4: Save changes and update the cache

After completing the modification of the template file, please make sure to save it.Then, log in to the AnQi CMS backend, find the 'Update Cache' feature, click 'Clear System Cache', so that your modifications can take effect on the front page.Then, visit your website's frontend and check if the article summary has been restricted to display by word count.

Advanced considerations and **practice**

  • Choice of word countIt is very important to choose a suitable number of words.This depends on your website design, the nature of the article content, and the amount of information you want to convey.Suggest trying several values to find the one that best balances beauty and information delivery.
  • Manual entry of the introduction's priorityEven if the template filter for word truncation is set, the content manually filled in the 'Document Summary' field still has the highest priority.If the content filled manually is sufficiently refined, the filter will not cut it again.Therefore, for especially important articles, writing a precise summary manually is still**practical**.
  • Special characteristics of multilingual content:truncatewordsThe filter identifies "words" primarily through spaces.For English and other languages separated by spaces, its effect is very intuitive.truncatewordsMay treat a continuous segment of Chinese text as a single "word".