How to display the total word count of all Tag tags in a document within AnQiCMS?

Calendar 👁️ 73

In the daily operation of AnQiCMS, we often need to conduct detailed analysis and optimization of content.For SEO and content strategy, it is a basic and practical need to understand the word count and word count of each element in the document.Today, we will discuss a specific scenario: how to display the total word count of all associated Tag tags in an AnQiCMS document.This can not only help us better evaluate the quality and relevance of tags, but also be used in certain specific content display needs.

AnQiCMS provides a powerful and flexible template tag and filter mechanism, allowing you to easily implement various complex data display logic on the front end.To solve the problem of counting the total number of words in tags, we need to integrate several core functions: get document tags, calculate the number of words in strings, and sum these values.

Understand the tags and filters of AnQiCMS

Before delving into the code, let's briefly review the relevant concepts.AnQiCMS allows you to add one or more Tag tags to each document (see "Document Tag Usage Help").These tags are usually used for content categorization and indexing, helping users find related content.In the template, we can go throughtagListTags to get all the tags associated with a document.

And the calculation of word count requires the use of the built-in "filter" function of AnQiCMS. EspeciallywordcountA filter that can help us count the number of words in a given string. Moreover,setTags are used to define variables in templates, andaddThe filter is used to add numbers, which is crucial for summing the word count of multiple tags.

Implementation steps: Count the total word count of tags.

Assuming we are editing the detail page template of a document (usuallyarchive/detail.htmlOr a file with a similar name), and you want to display the total word count of all Tag tags on this page. Here are the specific steps and code examples.

Step 1: Get the list of all tags in the current document

You can usetagListTags to get all the tags associated with the current document. If you are on the document details page,itemIdThe parameter will automatically retrieve the ID of the current document, no manual specification is required.

{% tagList tags %}
    {# 标签列表将通过 'tags' 变量提供 #}
{% endtagList %}

Here, tagsThe variable will contain an array, where each element is a tag object, containing the tag's ID, title (Title), links and other information.

Step two: Traverse the tags and calculate the number of words in each tag title.

Next, we need to traverse.tagsThe array, count words for each tag title. The tag title can be accessed byitem.TitleGet it.wordcountThe filter can be applied to this title string.

At the same time, we need a variable to accumulate the word count of each tag. In the AnQiCMS template, you can usesettags to define a variable, and useaddThe filter performs an accumulation operation.

{% set totalWords = 0 %} {# 初始化一个变量来存储总单词数 #}

{% tagList tags %}
    {% for item in tags %}
        {# 获取当前标签的标题,并计算其单词数 #}
        {% set tagWordCount = item.Title|wordcount %}
        
        {# 将当前标签的单词数累加到总数中 #}
        {% set totalWords = totalWords|add:tagWordCount %}
    {% endfor %}
{% endtagList %}

{# 在这里,totalWords 将包含所有标签的总单词数 #}

In the above code snippet,totalWordsit will first be initialized to 0. Then,forthe loop will process each one by one.tagsEach tag in the array. For each tagitemWe extract its titleitem.TitleAnd then|wordcountThe filter gets the number of words in the title and assigns it totagWordCountFinally, use|add:tagWordCountAdd this number tototalWordsthe variable.

a complete template code example

Integrate the above logic into your document detail page template, the final implementation code may look like this:

<article>
    <h1>{{ archive.Title }}</h1> {# 假设这是文档标题 #}

    {# 其他文档内容部分 #}
    <div>
        {{ archive.Content|safe }}
    </div>

    <section class="document-tags-wordcount">
        <h3>文档标签总览</h3>
        
        {# 初始化一个变量来存储所有标签的单词总数 #}
        {% set totalWordsForTags = 0 %}

        {# 获取当前文档的所有Tag标签列表 #}
        {% tagList currentDocumentTags with itemId=archive.Id %}
            {% if currentDocumentTags %}
                <p>当前文档关联的标签及单词数:</p>
                <ul>
                {% for tagItem in currentDocumentTags %}
                    {# 计算单个标签标题的单词数 #}
                    {% set individualTagWordCount = tagItem.Title|wordcount %}
                    
                    {# 累加到总单词数变量中 #}
                    {% set totalWordsForTags = totalWordsForTags|add:individualTagWordCount %}

                    <li>
                        <a href="{{ tagItem.Link }}" title="查看与 {{ tagItem.Title }} 相关的文档">
                            {{ tagItem.Title }}
                        </a>
                        (包含 {{ individualTagWordCount }} 个单词)
                    </li>
                {% endfor %}
                </ul>
                <p><strong>所有标签标题的总单词数量:{{ totalWordsForTags }} 个单词。</strong></p>
            {% else %}
                <p>该文档目前没有关联任何标签。</p>
            {% endif %}
        {% endtagList %}
    </section>

    {# 文档的上一篇/下一篇等其他内容 #}
    {% prevArchive prev %}
        {% if prev %}<a href="{{ prev.Link }}">上一篇:{{ prev.Title }}</a>{% endif %}
    {% endprevArchive %}
    {% nextArchive next %}
        {% if next %}<a href="{{ next.Link }}">下一篇:{{ next.Title }}</a>{% endif %}
    {% endnextArchive %}

</article>

Code explanation:

  1. {% set totalWordsForTags = 0 %}: We first define and initialize a variable namedtotalWordsForTagsThe variable used to store the total number of words added up.
  2. {% tagList currentDocumentTags with itemId=archive.Id %}: This tag is used to get all tags associated with the current document (viaarchive.Idspecified). We store the results incurrentDocumentTagsthis variable.
  3. {% for tagItem in currentDocumentTags %}: This is a loop, it will process one by onecurrentDocumentTagsEach label object in the array, each label in the loop istagItemrepresented.
  4. {% set individualTagWordCount = tagItem.Title|wordcount %}: Within the loop, we get the title of the current label (tagItem.Title),and use|wordcountThe filter calculates the number of words and assigns the result toindividualTagWordCount.
  5. {% set totalWordsForTags = totalWordsForTags|add:individualTagWordCount %}: This is a critical step in accumulation. We use|add:The filter willindividualTagWordCountto add its value tototalWordsForTagsto achieve the cumulative total of word count.
  6. <li>...</li>In the loop, we also displayed the title, link, and word count of each tag.
  7. <strong>所有标签标题的总单词数量:{{ totalWordsForTags }} 个单词。</strong>After the loop,totalWordsForTagsThe cumulative word count of all tag titles has been saved, you can display the final results here.

Thinking about practical application scenarios

Count the total number of words in document tags, it may sound like a niche requirement, but it can play a significant role in content operation and SEO analysis:

  • Content structure analysis:Understand that the 'word count' of a label can reflect the richness of its content to some extent. If the number of words in the label is too few, it may mean that the label is too broad or needs a more specific description.
  • SEO keyword density reference:Although it is not a direct keyword density calculation, it can be used as an indirect reference to measure the "weight" of the tag in the content, helping you optimize the tag strategy.
  • Uniform content specification:For teams with strict content publishing standards, statistics can be used to ensure that the naming of tags complies with internal length or complexity requirements.

Frequently Asked Questions (FAQ)

1.wordcountHow is a filter defined as a 'word'? Is it applicable to Chinese content?

wordcountThe filter mainly uses spaces to distinguish words. For example, "AnQiCMS is great" is counted as 3 words.For Chinese content, if there are no spaces between words, such as "Anqi Content Management System", it will be counted as a single word.If you want to count the number of each Chinese character, you need to uselengthA filter, it calculates the UTF-8 character count of a string.

2. Where can I use this code?

This code is best placed in the document detail page template of AnQiCMS, for exampletemplate/{您的模板目录}/archive/detail.html.

Related articles

How does the `wordcount` filter define words when processing strings containing non-ASCII characters (such as emojis)?

It is crucial to master the usage of various template filters when managing and presenting content in Anqi CMS, especially tools like `wordcount` that seem simple but may bring subtle differences.As our content becomes richer and no longer limited to pure text, the emergence of emojis and multilingual characters makes the definition of 'word' less intuitive.The `wordcount` filter in Anqi CMS, as the name implies, is used to count the number of words in a string.Its usage is very concise, whether it is directly applied to a variable, for example `{{

2025-11-09

How to use `wordcount` as a preliminary measure of content quality before publishing an article?

In content operation, publishing high-quality articles is the core to attract users and improve search engine rankings.AnQiCMS (AnQiCMS) provides rich features to help us manage and optimize content.Before publishing the article, using some preliminary indicators for inspection can effectively improve the quality of the content.Among them, word count (`wordcount`) is a simple yet practical preliminary measure.Why Word Count is So Important for Content Quality?The word count is not just a number; it plays a multiple role in content quality assessment: 1.

2025-11-09

The `wordcount` filter can be nested with other logical judgments (such as `for` loops)?

AnQiCMS provides a flexible and powerful template engine, allowing us to build website content with ease.In daily content display and data processing, there are often scenarios where you need to count the length of text, such as displaying the number of words in the abstract of an article list, or limiting the length of user comments.At this time, the `wordcount` filter comes into play.Many friends may be curious, whether the `wordcount` filter in the AnQiCMS template, in addition to being applied directly to variables, can also be used with other logical judgment tags, especially

2025-11-09

How to use `wordcount` to create dynamic styles based on content length in AnQiCMS template design?

In website content operation, we often encounter a challenge: how to keep the website interface beautiful and readable when displaying content of different lengths?A brief news report and an in-depth industry analysis, they should be distinguished in layout and style to provide a better user experience.AnQiCMS (AnQiCMS) fully understands this need and provides powerful and flexible tools in template design to solve this problem.Today, let's discuss how to use AnQiCMS's `wordcount` filter to create dynamic styles based on content length

2025-11-09

Can the `wordcount` filter distinguish text blocks embedded in the text and exclude them from the count?

In Anqi CMS, managing and displaying content is the core work of daily operations, among which, the statistics of the number of words in articles is a seemingly simple but may involve details problem.Today, let's delve into whether the `wordcount` filter can intelligently identify and exclude code blocks embedded in the content. ### The working principle of the `wordcount` filter Firstly, let's understand how the `wordcount` filter works in the AnQi CMS.Based on the document description

2025-11-09

How to add a `wordcount` statistic for a specific field in a custom content model and display it on the frontend?

## Easily implemented: Add word count and display on the front end for article content in the Anqi CMS custom content model Word count is a seemingly simple but very useful feature in content management.In order to help readers quickly assess reading time, optimize content length to meet SEO strategies, or to control article quality for internal operations, displaying the number of words in the article can provide a direct reference.AnQi CMS with its flexible content model and powerful template tag system makes this requirement accessible. Next

2025-11-09

Does the `wordcount` filter support counting the frequency of specific "word" like the `count` filter?

During the template development and content management process of AnQi CMS, we often need to perform various text analyses, including counting the vocabulary of text or the frequency of specific words.AnQi CMS provides us with a variety of practical template filters to handle such requirements.Today, let's discuss in detail the similarities and differences between the `wordcount` filter and the `count` filter in terms of word frequency.### Understanding the `wordcount` filter's function When you need to understand the overall length of a text, or how many 'words' it contains

2025-11-09

How to quickly view the output results of the `wordcount` filter for different strings while debugging AnQiCMS templates?

When developing or debugging templates in AnQiCMS, we often need to verify the output effect of specific data or filters (filter).Especially filters like `wordcount` that may have different counting logic for text in different languages and formats.To ensure that the template works as expected, it is particularly important to quickly view the output results of the `wordcount` filter for various strings.AnQiCMS's template system is based on syntax similar to Django

2025-11-09