When managing content in AnQi CMS, the display method of the article summary (also often 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 AnQi CMS defaults to automatically extracting the first 150 characters of the article content as a summary if the user does not manually fill in the summary.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 we want to maintain a more natural reading experience.
Understand the default introduction processing method of AnQi CMS
When you edit an article on the Anqi CMS backend, you will see a "Document Summary" field.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 from the main content of the article and default to extracting the first 150 'characters' to fill in.The term 'character' usually refers to a character, for Chinese content, one Chinese character is a character.For English content, a letter or symbol counts as one character.
This method of truncation based on the number of characters can meet the needs in many cases.However, it may sometimes lead to some undesirable results.For English content, 150 characters may contain many words, and sometimes we only want to keep the summary within about 20 words.On the contrary, 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, being able to limit the display of introductions by word count will greatly enhance the flexibility and professionalism of content presentation.
The core method to implement word count limitation: using template filters
AnQi 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 in the background, we can make use of the template in theFilter (Filters)To achieve this goal.
In the AnQi CMS template system, there are several very practical filters that can help us process text truncation, among which the most suitable one 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, which is plain text and does not contain any HTML tags.truncatewords_htmlFilterIf your article summary may contain HTML tags (such as bold, links, etc.), and you want to retain these HTML structures after truncation, then you should usetruncatewords_htmlThis filter will try to ensure the integrity of HTML tags while extracting text, to avoid page display errors caused by truncating tags.When using this filter, it is usually necessary to use in conjunction with|safeA filter that tells the template engine that this content is safe HTML and does not require additional escaping.
Operation steps with code examples
Now, let's see how these filters are applied in actual template files.
Step 1: Determine the template file to be modified
The article summary usually appears in multiple places 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 template file corresponding to displaying the article summary according to your website layout.These files are usually located in the template theme directory you are currently using (for example/template/你的主题名/), the filename may includeindex.html(Home page),archive/list.html(Article list page) or other custom list templates.
Step two: Locate the call location of the article summary
In the template file you find, you need to find the code used to display the article summary. Usually, this will be something similar to{{ item.Description }}or{{ archive.Description }}the variable call. Hereitemorarchiverepresents the current article object, whereasDescriptionfield is the brief content of the article.
Step 3: Applytruncatewordsortruncatewords_htmlFilter
Assuming 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 inside it:
{# 原始调用,可能显示过长的字符简介 #} <p>{{ item.Description }}</p> {# 修改后:限制显示20个单词 #} <p>{{ item.Description|truncatewords:20 }}</p>If the summary may contain HTML tags and you want to keep 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 usually needs to be used in a chain|safeFilter to ensure that HTML content is parsed correctly and not 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 to clear the system cache so that your changes can take effect on the front page.Next, visit the frontend of your website and check if the article summary has been restricted to display based on word count.
Advanced considerations and **practice.
- Word count selection.: Choosing the right number of words is very important. It 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 communication.
- The priority of manually filling in the introductionEven if the template filter is set to truncate words, the content manually filled in the "Document Introduction" field in the background still has the highest priority.If the content filled manually is sufficient concise, the filter will not truncate it again.Therefore, for especially important articles, writing a precise summary manually is still**practice.
- The particularity of multilingual content:
truncatewordsThe filter mainly identifies 'words' through spaces. Its effect is very intuitive for languages like English that are separated by spaces.But for languages such as Chinese and Japanese that do not have space-separated words,truncatewordsMay treat a continuous segment of Chinese text as a single "word",