How to calculate the number of occurrences of a specific keyword in the AnQiCMS template string or array?

Calendar 111

In daily website operations, we often need to analyze and manage content in detail.A common requirement is to count the number of occurrences of a specific keyword in a website template string or array.It is very helpful to understand how to implement this feature in AnQiCMS templates, whether it is for optimizing SEO keyword density, analyzing content hotspots, or for dynamically displaying relevant information on the page.

AnQiCMS benefits from its powerful and flexible Django-style template engine, providing rich built-in tags and filters, making these operations extremely simple.Next, let's take a look at how to easily calculate the number of occurrences of a specific keyword in the AnQiCMS template.

Core function:countFilter

AnQiCMS template engine built-in a very practicalcountA filter, its main function is to calculate the number of times a specific keyword appears in a string or array. This is the key to solving this problem.

Usage: countThe basic usage of the filter is very intuitive, you can apply it to the string or array variable you want to count, and pass the keyword you want to search as a parameter.

{{ obj|count:关键词 }}

HereobjCan be any string variable or array variable (slice in Go language).

Example demonstration:

Suppose you have a text introducing AnQiCMS, and you want to know how many times the word 'CMS' appears:

{% set description = "欢迎使用安企CMS(AnQiCMS),安企CMS是一款基于Go语言开发的企业级内容管理系统。" %}
<div>
    “CMS”在该描述中出现了:{{ description|count:"CMS" }} 次。
</div>

This code runs, the page will display: "CMS" appears 2 times in the description.

Similarly, if your variable is an array containing multiple elements,countThe filter can also work in the same way. For example, you have an array of page tags:

{% set tags_array = ["安企CMS", "SEO优化", "内容营销", "安企CMS功能"] %}
<div>
    数组中“安企CMS”出现了:{{ tags_array|count:"安企CMS" }} 次。
</div>

The result will be: 'AnQi CMS' appears 2 times in the array.

CombinesplitThe filter handles more complex scenarios.

In the process of template development, you may encounter situations where some data is obtained from the background in the form of strings, but these strings are actually connected by multiple keywords with a specific delimiter (such as a comma). At this time,countThe filter itself cannot understand that this is a "keyword list". However, AnQiCMS also providessplitA filter to solve this problem.

splitThe filter can split a string into an array according to a specified delimiter. Then, we can pass the split array tocountThe filter performs statistics.

Usage: splitThe basic usage of the filter is as follows:

{{ obj|split:分隔符 }}

Comprehensive example:

Assuming your article tags (tag) are stored in a comma-separated stringarchive.Tagsvariable, do you want to count the number of occurrences of "Go language":

{% set archive_tags_string = "AnQiCMS,Go语言,内容管理,SEO,Go语言" %}
{% set archive_tags_array = archive_tags_string|split:"," %}

<div>
    文章标签中“Go语言”出现了:{{ archive_tags_array|count:"Go语言" }} 次。
</div>

Run this code, the page will display: The tag 'Go language' appeared 2 times.

Even if the original data format does not match completely, it can be used in such a combination.countThe array form processed directly by the filter, we can also achieve keyword statistics through simple steps.

Application scenarios in practice

This keyword statistics function is widely used in the content operation of AnQiCMS:

  • SEO optimization: You can quickly check the density of core keywords in the article title, description, or正文, and assist the content team in adjusting SEO strategies.
  • Content Analysis: Understand the frequency of mention of a specific word in user-defined fields (such as product features, service advantages, etc.) to better understand the emphasis of the content.
  • Dynamic content display: Adjust the display strategy of related recommendations and ad placements on the page dynamically based on the frequency of certain keywords, to enhance user experience and content value.

The flexibility of the AnQiCMS template engine allows you to easily handle these seemingly complex content operation needs and transform technical information into practical display effects.


Frequently Asked Questions (FAQ)

Q1:countDoes the filter distinguish between uppercase and lowercase when counting?

A1: Yes,countThe filter is case-sensitive when performing keyword matching.For example, counting "CMS" and counting "cms" will yield different results.If you need to perform case-insensitive statistics, you can first useupperorlowerThe filter converts all content in the target string or array to uppercase or lowercase and then counts it. For example:{{ description|lower|count:"cms" }}.

Q2: How can I count the occurrence of keywords in the content of an article (output from a rich text editor HTML)?

A2: If the article content is edited through a rich text editor, it usually contains HTML tags. Use the string containing HTML tags directly.countThe filter may count text within HTML tags, or it may lead to inaccurate statistics. To accurately count pure text keywords, it is recommended that you first usestriptagsThe filter removes all HTML tags and then usescountThe filter. For example, get the content of the article and count the keywords:

{% archiveDetail articleContent with name="Content" %}
{% set clean_content = articleContent|striptags %}
<div>
    文章正文中“AnQiCMS”出现了:{{ clean_content|count:"AnQiCMS" }} 次。
</div>

Q3: I just want to know if a certain keyword exists, not how many times it appears, what filter should I use?

A3: If you are only concerned with the existence of keywords, not the specific number of occurrences, you can usecontaina filter. It will return a boolean value(TrueorFalse), indicates whether a keyword exists in a string or array. For example:

{% set text = "AnQiCMS让您的网站更安全" %}
{% if text|contain:"安全" %}
    <span>内容包含“安全”关键词。</span>
{% endif %}

Related articles

How to judge whether a string or array contains a specific keyword and display the result in AnQiCMS template?

When building and managing website content, we often encounter such needs: to decide whether to display a "hot" tag based on whether the article title contains a certain keyword; or check whether a certain feature is mentioned in the product description to adjust its display style.These seemingly subtle dynamic adjustments can greatly enhance the intelligence and user experience of the website.In AnQiCMS, due to its flexible Django template engine syntax, implementing such judgments is not complicated.

2025-11-07

How to use AnQiCMS filter to display strings centered or aligned to a specified length?

In website content display, we often need to format text to make it more beautiful and tidy in layout, especially in lists, tables, or areas that require a unified visual effect.AnQiCMS (AnQiCMS) powerful template engine provides a variety of practical filters (filters) that can help us easily achieve centered, left-aligned, or right-aligned strings and can be filled to a specified length.

2025-11-07

How to perform addition of numbers and strings in AnQiCMS template and display the result?

When building dynamic content in AnQi CMS templates, it is often necessary to perform simple calculations and concatenations of numbers or strings.Whether it is to display the cumulative user points or to combine and display product codes with specific prefixes, Anqi CMS provides a flexible and convenient way to meet these needs.This article will deeply explore how to perform addition operations in the AnQiCMS template, and provide detailed examples and practical suggestions.### Understanding the computational capabilities of AnQi CMS template The AnQi CMS template system is based on syntax similar to the Django template engine

2025-11-07

How to display the AnQiCMS page's canonical URL?

In website operation, we often encounter the situation where content appears repeatedly under different URLs.For example, a product may have multiple colors, each color generating an independent page, but their core content is the same.Or, in order to track traffic, websites may add various parameters to the URL.This "duplicate" content, although it has little impact on user experience, but it may confuse search engines, not knowing which is the "original version", thus possibly affecting the crawling efficiency and ranking of the website.

2025-11-07

How to remove the specified characters from a string in the AnQiCMS template to optimize display?

In website operation, the clarity of content presentation and user experience are crucial.Sometimes, the content we enter from the background or the fields we retrieve from the database may contain some unnecessary characters, such as extra spaces, specific delimiters, or leading text that needs to be cleaned up, all of which may affect the aesthetics of the page and the readability of the content.

2025-11-07

How to convert a numeric string in AnQiCMS template to a floating-point number or integer for display?

In AnQiCMS template development, data processing is one of the core links.The flexible content model allows us to customize various fields to store website information, including scenarios where numbers are needed, such as product prices, inventory quantities, ratings, and so on.Although these numbers may look normal in the background, they are likely to be presented as strings when called in the template.This brings up a common question: How can we convert these numeric strings to floating-point numbers or integers in a template for mathematical operations or specific formatting?

2025-11-07

How to link array elements into a string for display in AnQiCMS template?

In AnQiCMS template development, we often encounter situations where we need to display a series of related data, such as multiple tags of an article, various characteristics of a product, or multiple options stored in a custom field.These data are often present in the template in the form of an array, and we hope to display them in a concise and beautiful string format, such as connecting them with commas, slashes, or other symbols.

2025-11-07

How to extract a specified part of a string or array for display in the AnQiCMS template?

In AnQiCMS template development, we often encounter the need to handle string or array content, such as displaying article summaries, limiting the number of image lists, or extracting specific information from a long text.The AnQi CMS is developed based on the Go language, its template engine supports syntax similar to Django and Blade, and provides a rich set of filters (filters) to help us efficiently complete the content extraction and display requirements.Let's take a look at how to flexibly extract the specified part of a string or array for display in the AnQiCMS template.

2025-11-07