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.