When operating a website, we often need to understand some basic information about articles, such as reading time and word count.This data can not only help us optimize our content strategy, but also allow readers to have a preliminary judgment of the article while browsing.In the AnQi CMS, by utilizing its powerful template filter function, we can easily display the word count of each article on the article list page.
The template engine of AnQi CMS provides a rich set of filters (filters), which act like a toolbox, allowing various processing of variables in the template, such as formatting dates, truncating strings, of course, including the one we are discussing today——word count. The syntax for using filters is very intuitive, usually adding a pipe symbol after the variable|Then follow the filter name, if parameters are needed, add a colon after the filter name:and parameters.
The core tool for counting the number of words in articles iswordcountFilter. As the name suggests,wordcountThe function of the filter is to count the number of words or characters in the text.It will traverse the input string and count the number of 'words' based on separators such as spaces.For Chinese content, a character is usually considered as a 'word' or at least a counting unit; for English content, it is counted by words.
To display the word count of each article on the article list page, we need to edit the corresponding template file of the list page. These template files are usually located at/template/{您的模板目录}/{模型table}/list.htmlor a similar path (for exampledefault/article/list.html).
The steps to implement this feature are as follows:
Firstly, we need to usearchiveListTags can be used to obtain a list of article data. This tag can help us retrieve articles that meet certain conditions from the database and organize them into a collection that can be iterated over by the template.archiveList标签内部,我们会使用 Englishfor循环来逐一处理每一篇文章。
Infor循环中,每篇文章的数据都会被赋值给一个变量(通常是 English)item)。文章的内容存储在 Englishitem.ContentThis field is. At this time, we just need to simplywordcountapply theitem.Contentto get the number of words in the article.
For example, you can add the following code snippet to the article list page template:
{# 假设这是您的文章列表页模板文件,例如:default/article/list.html #}
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<article class="article-item">
<h2><a href="{{item.Link}}">{{item.Title}}</a></h2>
<div class="article-meta">
<span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
<span>浏览量:{{item.Views}}</span>
{# 这里是核心:使用 wordcount 过滤器统计文章字数 #}
<span>字数:{{ item.Content|wordcount }}</span>
</div>
<div class="article-description">
<p>{{item.Description}}</p>
</div>
<a href="{{item.Link}}" class="read-more">阅读全文</a>
</article>
{% empty %}
<p>暂时没有文章发布。</p>
{% endfor %}
{# 如果您使用了分页功能,还需要添加分页代码 #}
{% pagination pages with show="5" %}
{# 这里放置您的分页链接代码,例如: #}
<div class="pagination-links">
{% if pages.FirstPage %}<a href="{{pages.FirstPage.Link}}">首页</a>{% endif %}
{% if pages.PrevPage %}<a href="{{pages.PrevPage.Link}}">上一页</a>{% endif %}
{% for page_item in pages.Pages %}
<a class="{% if page_item.IsCurrent %}active{% endif %}" href="{{page_item.Link}}">{{page_item.Name}}</a>
{% endfor %}
{% if pages.NextPage %}<a href="{{pages.NextPage.Link}}">下一页</a>{% endif %}
{% if pages.LastPage %}<a href="{{pages.LastPage.Link}}">尾页</a>{% endif %}
</div>
{% endpagination %}
{% endarchiveList %}
In this code,{{ item.Content|wordcount }}This is the key to implementing word counting. It retrieves the content of the current article.item.Contentand then throughwordcountThe filter calculates the word count and displays it directly. This way, you can add a practical and informative element to your article list, enhancing user experience.
Common Questions (FAQ)
Q1: Why did the front page not take effect immediately after I modified the template file and added awordcountfilter?
A1:The AutoCMS often enables template caching to improve website access speed.When you modify the template file, the browser may still load the old cached file, or the server may not have updated the cache in a timely manner.You can try to clear the cache in the "System Settings" or "Update Cache" feature of the Anqi CMS backend, and then force refresh the browser (Ctrl+F5 or Cmd+Shift+R).
Q2:wordcountFilter statistics: is it the number of Chinese characters or English words? Will the presence of HTML tags in the article content affect the statistical results?
A2: wordcountThe filter will differentiate "words" based on spaces in the text for counting.For English, it can accurately count the number of words; for Chinese, because there are usually no spaces between Chinese characters, it often counts the number of individual characters.item.ContentContains a large number of HTML tags (such as<p><strong>内容</strong></p>)wordcountThe filter will also count these tag texts, which may not be the 'visible word count' you want. To get a more accurate pure text word count, you canwordcountbefore usingstriptagsFilter, for example{{ item.Content|striptags|wordcount }}This can be used to remove all HTML tags first, and then count the number of characters in plain text.
Q3: Can I use it in other places besides the article list page?wordcountFilter? For example, the article detail page?
A3:Of course. As long as you can get the string variable containing the article content in the template, you can use it.wordcountFilter. In the article detail page (usuallydefault/article/detail.htmlor similar files), the article content is generally obtained througharchive.Contentor{% archiveDetail with name="Content" %}You can directly apply it to the detail page, for example文章字数:{{ archive.Content|wordcount }}To display word count information at the top or bottom of the article.