In content operations, understanding the frequency of keywords in articles is an important part of SEO optimization and content strategy analysis.By counting the frequency of keywords, we can better evaluate the density, relevance of content, and even discover potential optimization opportunities.AnQiCMS provides a powerful and flexible template system, combined with its built-in content filter, we can easily count the number of occurrences of specific keywords in the article content.

Understanding the demand: Why do we need to count keywords?

Count the number of times a keyword appears in an article, and it has the following practical application scenarios:

  1. SEO optimization evaluation:Search engines consider the frequency of keywords when determining the relevance of articles.Although 'keyword density' is no longer the only ranking factor, understanding its distribution is still helpful for content strategy.
  2. Content quality analysis:The frequency of keywords that is too high or too low may affect the reading experience. Moderate statistics can help us maintain the natural fluency of the content and avoid keyword stacking.
  3. Internal Linking StrategyIn some automated internal link tools, analyzing keywords helps identify which words can be turned into links, pointing to other related pages.
  4. Competitive Analysis:Analyze the keyword frequency in competitors' articles to provide reference for your content creation.

The core tool in AnQiCMS templates: content filter

AnQiCMS's template syntax is flexible and powerful, it draws on the advantages of template engines like Django or Blade, supporting basic operations such as variables, conditional judgments, and loops.It also provides rich content filters (Filters), which can process the output data in various ways, such as formatting time, truncating strings, or even counting characters or word numbers.To count the frequency of keywords, we mainly use two key points: obtaining the article content, and applying the statistical filter.

Get article content:archiveDetailtags

Firstly, we need to get the full content of the article in the template. In the article detail page template of AnQiCMS, various data of the current article can usually be accessed directly througharchive.字段名of the form access. The main content field of the article is usuallyarchive.Content.

If you need to get the content of a specified article on a non-article detail page (such as the homepage or list page), you can usearchiveDetailLabel. However, for our current needs - counting keywords in the 'article content', it usually means we are on the article detail page, and using it directly is the most convenient way.archive.Contentis the most convenient way.

For example, inarchive/detail.htmlsuch article detail template, we can directly access:

{{ archive.Content|safe }}

Here are the|safeThe filter is to ensure that the HTML tags in the article content can be correctly parsed and displayed, rather than output as plain text.

Count the number of keywords:countFilter

With the article content, the next step is to count the frequency of keyword occurrences. AnQiCMS provides a very practicalcountThe filter to complete this task. This filter can calculate the frequency of a keyword in a string.

countThe basic usage of the filter is as follows:

{{ obj|count:关键词 }}

Among them:

  • objIt is the string object that we need to count, for examplearchive.Content.
  • 关键词It is the target string that we need to count.

For example, if we want to count how many times the word 'CMS' appears in a text:

{% set text = "欢迎使用安企CMS(AnQiCMS),这是一个基于Go语言的CMS。" %}
{% set cms_count = text|count:"CMS" %}
<p>关键词 "CMS" 在文本中出现了:{{ cms_count }} 次。</p>

The output of this code will be:关键词 "CMS" 在文本中出现了:2 次。

Combine them together: complete implementation steps

Now, we combine the steps of getting article content and usingcountcombining the two steps of filter. Suppose we are editing a template for an article detail page (for example/template/default/archive/detail.htmlOr custom an article template), and want to count the number of times a certain keyword appears in the current article content.

You can follow the steps below:

  1. Define the keyword to be countedTo facilitate management and modification, we can useseta tag to define a variable to store the keywords we want to count.
  2. Get article content: Utilizingarchive.ContentGet the full content of the current article.
  3. ApplycountFilter:Take the article content as filter input, keywords as parameters, and get the statistical results.
  4. Display the results:Display the statistical results on the page.

The following is a complete code example demonstrating how to count the occurrences of the keyword 'AnQiCMS' in the current article content within the AnQiCMS template.

{# 1. 定义要统计的关键词 #}
{% set keyword_to_count = "AnQiCMS" %}

{# 2. 检查文章内容是否存在,以避免不必要的错误 #}
{% if archive.Content %}
    {# 3. 对文章内容应用 'count' 过滤器进行关键词统计 #}
    {% set count_result = archive.Content|count:keyword_to_count %}

    {# 4. 在页面上展示统计结果 #}
    <div class="keyword-stats">
        <p>关键词 "{{ keyword_to_count }}" 在本文中出现了:<strong>{{ count_result }}</strong> 次。</p>
    </div>
{% else %}
    <div class="keyword-stats">
        <p>文章内容为空,无法统计关键词出现次数。</p>
    </div>
{% endif %}

Insert this code snippet into an appropriate position in your article detail template, such as below the article title, at the end of the text, or in the sidebar. The page will automatically calculate and display the number of times the keywords appear when the page is loaded.

Practice Exercise: A Specific Example

假设我们有一篇关于 “AnQiCMS 使用教程” 的文章,内容中多次提到了 “AnQiCMS” 和 “Go 语言”。We hope to display the statistics of these two keywords at the bottom of the article.

”`twig {# This is a part of the article detail page template #}

<h1>{{ archive.Title }}</h1>
<div class="article-meta">
    发布日期: {{ stampToDate(archive.CreatedTime, "2006-01-02") }} | 浏览量: {{ archive.Views }}
</div>

<div class="article-content">
    {{ archive.Content|safe }}
</div>

<div class="keyword-analysis">
    <h3>关键词统计分析:</h3>
    {% set keywords_to_analyze = ["AnQiCMS", "Go语言", "模板"]|list %} {# 定义一个关键词列表 #}
    {% set