How to easily count the word count of article content in AnQiCMS templates? Practical methods and techniques

In content operations, understanding the word count or the number of words in an article is a basic and important indicator.It is very helpful to accurately calculate the length of article content, whether it is for SEO optimization, evaluating the cost of content creation, or ensuring that the content complies with platform standards.AnQiCMS provides us with a simple yet powerful way to directly implement this requirement in templates, thanks to its flexible template engine.

The template system of AnQi CMS is based on the Django template engine syntax, providing rich tags and filters (filters) that can help us easily handle various types of data. To accurately calculate the number of words in the content of an article, we mainly use the following.wordcountFilter.

UtilizewordcountFilter statistics word count

wordcountThe filter is a very practical feature built into AnQiCMS templates, specifically designed to calculate the number of words separated by spaces in a string. Its usage is intuitive and simple: you just need to pass the string to be counted through the pipe character|Pass towordcountthe filter.

For example, if you are viewing the details page of a document and want to count the number of words in the main text of the document, you can do it like this:

{{ archive.Content|wordcount }}

Here,archive.ContentIt represents the main text content of the current document.|wordcountThe content will be taken as input, and the total number of words it contains will be returned. The result will be an integer.

Get the article content for statistics

The article content is usually stored in documentsarchive)、Single Page(page)Categories(`category)or Tags(tag) ofContentfield. To retrieve and count these contents, we need to use the corresponding detail tags such asarchiveDetail/pageDetailExtract content to a variable or apply a filter directly to the field.

Assuming we want to count the number of words in a specific article (ID 1), the complete template code would be like this:

{% archiveDetail articleData with name="Content" id="1" %}
    <p>文章标题:{% archiveDetail with name="Title" id="1" %}</p>
    <p>文章内容单词总数:{{ articleData|wordcount }}</p>
{% endarchiveDetail %}

Here, we first usearchiveDetailtags to refer to the article with ID 1ContentContent is fetched and assigned toarticleDatathe variable. Then, we can applyarticleDatathis variable towordcounta filter to count the number of words.

The effect of HTML tag pairs on the count.

It is worth noting that the article content in AnQiCMS is usually in rich text format, containing a large number of HTML tags (such as<p>,<strong>,<img>If applied directly to a string with HTML tagswordcountFilter, which may lead to inaccurate statistical results, as it includes the text even part of the tag and the tag itself.

To obtain a more accurate word count, we should removewordcountUse it before the filterstriptagsall HTML tags from the content.striptagsThe filter can strip HTML, XML, and PHP tags to ensure that we only count words in plain text.

CombinestriptagsThe precise counting method after filtering is as follows:

{% archiveDetail articleContent with name="Content" %}
    {% set pureTextContent = articleContent|striptags %}
    <p>文章纯文本单词总数:{{ pureTextContent|wordcount }}</p>
    <p>文章纯文本字符总数:{{ pureTextContent|length }}</p>
{% endarchiveDetail %}

This is,pureTextContentThe variable only contains the plain text content of the article, and then it iswordcountcounted to get a more accurate word count. At the same time, I also showedlengthA filter that can be used to count the total number of characters, which is more relevant in some scenarios (such as counting the 'word count' of Chinese articles) than the number of words.

Actual application example

Suppose you want to display the word count and character count of the article at the bottom of the article detail page, your template file ({模型table}/detail.html) may contain the following code snippet:

<article>
    <h1>{{ archive.Title }}</h1>
    <div class="article-meta">
        <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
        <span>浏览量:{{ archive.Views }}</span>
    </div>

    <div class="article-content">
        {% archiveDetail articleBody with name="Content" %}
        {{ articleBody|safe }} {# 这里使用 |safe 确保HTML内容被正确渲染 #}
        {% endarchiveDetail %}
    </div>

    <div class="article-stats">
        {% archiveDetail articleRawContent with name="Content" %}
            {% set cleanedContent = articleRawContent|striptags %}
            <p>本文纯文本单词数:{{ cleanedContent|wordcount }}</p>
            <p>本文纯文本字符数:{{ cleanedContent|length }}</p>
        {% endarchiveDetail %}
    </div>
</article>

This example clearly demonstrates how to retrieve article content, clean HTML tags, and then count the number of words and characters separately.In this way, you can flexibly and accurately obtain the length information of article content in the AnQiCMS template, thus better managing and operating the website content.


Common Questions (FAQ)

  1. wordcountandlengthWhat are the differences between filters? wordcountThe filter is mainly used to count the number of 'words' separated by spaces, more suitable for English or other languages that use spaces to separate words.lengthThe filter is used to count the number of 'characters' in a string, including letters, numbers, symbols, and Chinese characters, etc., with each Chinese character counting as 1 character. For Chinese content, it is usuallylengthIt is more common and accurate to use a filter to count 'word count'.

  2. Does the inclusion of images or video rich media elements in the article content affect word counting?If the HTML tags (including images, videos) in the article content<img>/<video>are not removed,wordcountThe filter may incorrectly calculate some text or attribute values within these tags as words, leading to inaccurate statistics. To obtain an accurate count of pure text words, it is recommended to applywordcountUse it before the filterstriptagsFilter removes all HTML tags.

  3. wordcountIs the filter effective for Chinese content? wordcountThe filter mainly uses spaces to distinguish words, while Chinese text usually does not use spaces to separate vocabulary. Therefore, the content is used directly.wordcountThe filter often does not yield a meaningful number of "words". For Chinese content, it is more recommended to uselengthFilter to count the "number of characters