How to accurately calculate the word count of article content in AnQiCMS templates?

Calendar 👁️ 87

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.

Related articles

How to remove extra blank lines or newline characters in the output content by using the `remove` tag in AnQiCMS?

While developing website templates with AnQiCMS, we often encounter a detail issue that the logic tags (such as `if` condition judgment, `for` loop) in the template may generate unnecessary blank lines or newline characters when rendering output HTML content. These seemingly harmless blanks usually do not affect the final visual presentation of the page, but in some scenarios where code neatness is required or precise control of element spacing is needed, they may bring some minor troubles, such as affecting the readability of the HTML source code

2025-11-09

How to accurately control the timestamp date format display in the `stampToDate` tag of AnQiCMS on the front-end?

Clear, consistent, and beautiful time information display is crucial for user experience in website operation.Whether it is the publication date of the article, the update time of the product, or the deadline of the event, a friendly date and time format always allows visitors to obtain information more quickly.AnQiCMS deeply understands this, providing us with a powerful and flexible template tag - `stampToDate`, which allows us to easily control various date and time formats displayed on the front end.

2025-11-09

How to customize and display category banner images or thumbnails in AnQiCMS to enhance the visual effect?

In website operation, the application of visual elements is crucial for attracting users and enhancing brand image.The category page is an important entry for users to browse content, and if it is equipped with a dedicated Banner image or thumbnail, it will undoubtedly greatly enhance the aesthetics and professionalism of the page.Next, let's see how to achieve this effect in AnQiCMS. ### One, Backend Settings: Configure Exclusive Visual Elements for Categories To add exclusive visual elements to a category, we first need to enter the AnQiCMS backend management interface.

2025-11-09

Does AnQiCMS support displaying a custom shutdown prompt when the website is closed?

The website is in operation, and it is inevitable that there will be situations where the website needs to be temporarily closed, such as system upgrades, data maintenance, or business adjustments, etc.How can you clearly and professionally convey the current status of the website to visitors to avoid them seeing a stiff error page, which is particularly important.For users using AnQiCMS, the good news is that the system has fully considered this point and provided a very flexible setting function for the shutdown prompt information.Yes, AnQi CMS fully supports displaying your custom prompt information when the website is closed.

2025-11-09

How does the `wordcount` filter define the boundary of a 'word' when counting Chinese content?

In daily website content operation, we often need to count and control the length of content, which is crucial for SEO, layout and user reading experience.The AnqiCMS provides a series of practical template filters to help us complete these tasks, where `wordcount` is a tool used to count the number of 'words' in a string.However, how is the boundary of the 'word' defined for Chinese content?This may be a question that many users encounter when using it.

2025-11-09

How to use the `wordcount` filter to display the word count of each article on the article list page?

When operating a website, we often need to understand some basic information about articles, such as reading time, word count, etc.This data not only helps us optimize the content strategy, but also allows readers to have a preliminary judgment of the article while browsing.In Anqi CMS, by using its powerful template filter function, we can easily display the word count of each article on the article list page.

2025-11-09

AnQiCMS blog article detail page, how to display the total word count of the current article in real time?

In content operation, the word count (or number of words) of an article is often an important indicator for measuring the depth of content, the estimated reading time, and even search engine optimization (SEO).For those of us who use AnQiCMS to manage website content, being able to display the total word count of the current article in real-time on the blog article detail page would undoubtedly enhance user experience and provide more intuitive data reference for website operation.AnQiCMS powerful template system and built-in features make it simple and efficient to meet this requirement.

2025-11-09

What is the core difference between the `wordcount` filter and the `length` filter in calculating text length?

In AnQi CMS template development, we often need to process and count text content, including calculating the length of the text.The system provides the `wordcount` and `length` filters, which both help us measure text, but they have different focuses and application scenarios.Understanding the core differences can make us more proficient in practical use, writing more efficient and accurate template code.

2025-11-09