How to easily count the number of words in article content in AnQiCMS templates? Practical methods and skills

In content operation, understanding the number of characters or words in an article is a basic and important indicator.It is very helpful to accurately count the length of the article content for SEO optimization, content creation cost assessment, or to ensure that the content conforms to the platform standards.AnQiCMS (AnQiCMS) provides us with a simple and powerful way to directly implement this requirement with its flexible template engine.

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

UtilizewordcountFilter counts the number of words

wordcountThe filter is a very practical feature built into the AnQiCMS template, specifically designed to calculate the number of words in a string separated by spaces. Its usage is intuitive and simple: simply pass the string to be counted through the pipe symbol|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.ContentRepresents the main text content of the current document.|wordcountThis 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 content of the article for statistics

The content of the article is usually stored in a documentarchive)、Single page(page),category(`category)or tag(tag)的Contentfield. To obtain this content and perform statistics, we need to use the corresponding detail tags such asarchiveDetail/pageDetailExtract content to a variable or apply a filter directly.

Suppose 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 usearchiveDetailthe tag, to mark the article with ID 1'sContentContent is retrieved and assigned toarticleDataa variable. Then, we can use aarticleDatathis variable.wordcountfilter to count the number of

words affected by HTML tags.

It is worth noting that the content of articles 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 containing HTML tagswordcountThe filter may lead to inaccurate statistical results as it will calculate even part of the text in the tags and even the tags themselves.

In order to get a more accurate word count, we should applywordcountbefore the filter.striptagsThe filter will remove all HTML tags from the content.striptagsThe filter can strip HTML, XML, and PHP tags to ensure we only count pure text words.

CombinestriptagsThe precise counting method after the filter is as follows:

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

Thus,pureTextContentThe variable only contains the plain text content of the article, and then it performswordcountStatistics can 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 than word count in some cases (such as counting the 'word count' of Chinese articles).

Actual application example

Assuming you want to display the word count and character count 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 the content of an article, 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 the article content in the AnQiCMS template, thereby better managing and operating website content.


Frequently Asked 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. AndlengthA filter is used to count the number of 'characters' in a string, including letters, numbers, symbols, and Chinese characters, with each Chinese character counting as 1 character. For Chinese content, it is usually usedlengthThe filter to count 'word count' is more common and accurate.

  2. Will the content of the article, including images or video elements, affect word count?If the HTML tags (including images, videos) in the article content have not been removed,<img>/<video>these tags have not been removed,}wordcountThe filter may incorrectly calculate some parts of the text or attribute values within these tags as words, leading to inaccurate statistical results. To obtain an accurate count of pure text words, it is recommended to applywordcountbefore the filter.striptagsThe filter 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 words. Therefore, it is directly used for Chinese content.wordcountFilters often cannot get a meaningful number of 'words'. It is recommended to use for Chinese content.lengthA filter to count the 'number of characters' in an article, which is usually the conventional understanding of 'word count' in the Chinese context.