In content operations, the number of words (or word count) in an article is often an important indicator for measuring the depth of content, estimated reading time, and even search engine optimization (SEO).For those of us who use AnQiCMS to manage website content, if the total word count of the current article can be displayed in real time on the blog article detail page, it will undoubtedly improve user experience and also provide more intuitive data references for website operations.AnQiCMS powerful template system and built-in features make it simple and efficient to meet this requirement.
AnQiCMS's flexibility in templates and content acquisition
AnQiCMS with its concise and efficient architecture provides highly flexible content management capabilities.It uses a template engine syntax similar to Django, making it very intuitive to customize the way content is presented.For the blog article detail page, we usually usearchiveDetailTags to get various details of the current article, including the core contentContentfield.
For example, to get the article content on the article detail page, we will write the template code like this:
{% archiveDetail articleContent with name="Content" %}
{{ articleContent|safe }}
HerearticleContentThe variable carries the HTML content of the article processed by the rich text editor and|safeThe filter ensures that this HTML content is correctly parsed and displayed by the browser, rather than being output as plain text.
Skillfully use built-in filters:wordcount
The AnQiCMS template system not only provides a rich set of tags for data calls, but also integrates a variety of practical filters, which can further process and format variables. Among many filters,wordcountThe filter is the key to solving the need for 'real-time display of the total number of words in an article.'
wordcountThe filter's function is very direct: it is used to count the number of words in a string.It is worth mentioning that this filter can intelligently identify and ignore HTML tags when counting, which means it counts the words of actual text content rather than characters including tags.This is from the rich text editor obtainedContentFor fields, it is the most ideal tool.
Practical exercise: Step by step to implement word counting
Next, we will perform specific operations, integrating the word count feature into the AnQiCMS blog article detail page.
Locate the template file:According to AnQiCMS template design conventions, the template file for the article detail page is usually located in your template directory, for example
archive/detail.htmlor{模型table}/detail.html. You need to find this file and edit it.Get article content:In the template file, make sure you have passed
archiveDetailthe tag to get the article'sContentfield. If you just want to count the number of words without rendering the entire content, you can also get it separatelyContent.apply
wordcountFilter:The article content variable obtained, through the pipe character|连接wordcounta filter. For example:{% archiveDetail articleContent with name="Content" %} <p>文章总单词数:<span>{{ articleContent|wordcount }}</span> 词</p>Insert this code at any position where you want to display the number of words in an article. Typically, this is placed below the article title, next to author information, or at the bottom of the article in a prominent location.
Through such settings, when the user accesses the article detail page, the AnQiCMS template engine will automatically fetch the article content, utilizing
wordcountThe filter calculates the actual number of text words and displays it on the page. The entire process is seamless, requiring no additional backend development or complex frontend scripts.
Optimization and注意事项
- Automatic processing of HTML tags:
wordcountThe filter will intelligently strip HTML tags during counting, so you don't have to worry.<p>/<a>The tags will interfere with the final word count. This ensures the accuracy of the statistical results. - Performance consideration: Word count is a lightweight operation, the high-performance architecture of AnQiCMS based on Go language can easily handle it.Even for long content articles, the cost of real-time calculation is negligible and will not significantly affect page loading speed.
- User Experience:Combine the number of words with other reading information (such as estimated reading time) to provide visitors with a more comprehensive overview of the article, helping them decide whether to invest time in reading, thus improving the overall website user experience.
By combining simple template tags and filters, AnQiCMS can easily achieve real-time display of the total word count of articles, which not only reflects the strong functionality of the system in content management but also provides more data support for content operators, helping to refine the operation of website content.
Frequently Asked Questions (FAQ)
1.wordcountHow does the filter count Chinese content?
wordcountThe filter mainly distinguishes words based on spaces. For English content, it counts words. For Chinese content, since Chinese words are usually not separated by spaces, sowordcountThe filter treats continuous Chinese text as a single "word" for counting, unless the text explicitly contains English words or punctuation symbols separated by spaces.If you need an accurate count of Chinese words, you may need to combine other methods (such as using a Chinese segmentation library on the front end with JavaScript, but this goes beyond the direct support of the AnQiCMS template) or consider counting characters.
2. Which filter should I use to count the number of characters in an article instead of the number of words?If you need to count the number of characters in an article (including Chinese, English, numbers, symbols, and all visible characters, excluding HTML tags), you can uselengthFilter. For example,{{ articleContent|length }}The total number of characters returned in the article content. For Chinese content, this is usually more reflective of the length than the word count.
3. Can I also display the word count for each article on the article list page?Certainly. On the article list page, you would typically usearchiveListLabel to display multiple articles. In the loop body, the value of each article can be obtained. You can callContentto get the complete article of thearchiveListin the looparchiveDetailto get the complete content of the current articleContentand then applywordcounta filter. For example:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
{# 获取单篇文章的完整内容并统计单词数 #}
{% archiveDetail fullContent with name="Content" id=item.Id %}
<p>单词数:{{ fullContent|wordcount }} 词</p>
{% endarchiveDetail %}
<p>{{item.Description}}</p>
{% endfor %}
{% endarchiveList %}
Please, frequently call in the article list pagearchiveDetailto get the fullContentAnd statistics are carried out, although feasible, but if the list items are very many, it may have a certain impact on performance.In a practical project, you can weigh its necessity or consider storing the word count as a custom field when the article is published.