In the daily operation of AnQiCMS, we often need to perform detailed analysis and optimization of the content.For SEO and content strategy, understanding the word count and word number of each element in the document is a basic and practical requirement.Today, we will discuss a specific scenario: how to display the total word count of all associated Tag tags of a document in AnQiCMS.This can not only help us better evaluate the quality and relevance of tags, but also be used in some 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 comprehensively utilize several core functions: obtaining document tags, calculating the number of words in a string, and summing these values.

Understanding AnQiCMS tags and filters

tagListTags to get all the tags associated with a document.

The calculation of the word count requires the use of the built-in "filter" function of AnQiCMS. EspeciallywordcountFilter, it helps us count the number of words in a given string. In addition,setTags are used to define variables in templates,addThe 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 a document detail page template (usuallyarchive/detail.html或类似命名的文件),并且希望在这个页面上显示当前文档所有Tag标签的总单词数。以下是具体的实现步骤和代码示例。

First step: Get the list of all tags in the current document

You can usetagListTo get all tags associated with the current document. If you are on the document detail page,itemIdThe parameter will automatically retrieve the current document ID without manual specification.

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

Here,tagsThe variable will contain an array where each element is a label object, including the label's ID, title (Title)、链接等信息。

第二步:遍历标签并计算每个标签标题的单词数

接下来,我们需要遍历tagsArray, count words for each tag's title. The title of the tag can beitem.Titleto get.wordcountfiltered and 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, andaddThe filter performs cumulative operations.

{% 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 be initialized to 0 first. Then,forThe loop will process one by onetagsEach label in the array.item, and we extract its title.item.Title, and proceed through|wordcountFilter gets the word count of the title and assigns it totagWordCount. Finally, use|add:tagWordCountAdd this number tototalWordsfilter to the variable.

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 (byarchive.Idspecified). We store the result incurrentDocumentTagsthis variable.
  3. {% for tagItem in currentDocumentTags %}: This is a loop, it will process one by one.currentDocumentTagsEach tag object in the array, each tag is processed in the loop.tagItemRepresents.
  4. {% set individualTagWordCount = tagItem.Title|wordcount %}: Inside the loop, we get the current tag's title (tagItem.Title) and used|wordcountFilter calculates the number of words and assigns the resultindividualTagWordCount.
  5. {% set totalWordsForTags = totalWordsForTags|add:individualTagWordCount %}: This is the key step of summation. We use|add:The filter willindividualTagWordCountto add its value tototalWordsForTagsto achieve the cumulative total number of words.
  6. <li>...</li>: In the loop, we also displayed each tag's title, link, and its own word count.
  7. <strong>所有标签标题的总单词数量:{{ totalWordsForTags }} 个单词。</strong>: After the loop ends,totalWordsForTagsJust saved the cumulative word count of all tag titles, you can make the final display here.

Thoughts on practical application scenarios

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

  • Content structure analysis:Understanding the 'vocabulary' of tags can reflect the richness of the tag content to a certain extent. If the number of tag words is too few, it may mean that the tag is too broad or requires 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 tags in the content, helping you optimize your tag strategy.
  • Unified content specification:For teams with strict content release specifications, this statistics can ensure that the naming of tags conforms to internal length or complexity requirements.

Common Questions and Answers (FAQ)

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

wordcountFilter is mainly used to distinguish words by spaces.For example, 'AnQiCMS is great' would be counted as 3 words.For Chinese content, if words do not have spaces between them, such as "Anqi Content Management System", it will be counted as a single word.lengthThe filter, it calculates the UTF-8 character count of the string.

2. Where can I use this code?

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