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 %}