In daily website operations, we often need to conduct detailed analysis and management of content.One common requirement is to count the number of occurrences of a specific keyword in a string or array within a website template.It is very helpful to understand how to implement this function in AnQiCMS template, 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 a rich set of built-in tags and filters, making these operations extremely simple.Next, let's see how to easily calculate the frequency of specific keywords in AnQiCMS templates.
Core Function:countFilter
AnQiCMS template engine built-in a very practicalcountFilter, its main function is to calculate the number of times a specific keyword appears in a string or array. This is exactly 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:关键词 }}
Here are theobjIt can be any string variable or array variable (slice in Go language).
Example demonstration:
Assuming you have a description of AnQiCMS, how many times does the word 'CMS' appear in it?
{% set description = "欢迎使用安企CMS(AnQiCMS),安企CMS是一款基于Go语言开发的企业级内容管理系统。" %}
<div>
“CMS”在该描述中出现了:{{ description|count:"CMS" }} 次。
</div>
This code runs, and the page will display: 'CMS' appears 2 times in this 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: '安企CMS' appears 2 times in the array.
CombinesplitThe filter handles more complex scenarios
In practical template development, you may encounter situations where some data is obtained from the background in the form of strings, but these strings are actually multiple keywords connected by a specific delimiter (such as a comma). At this time,countThe filter itself cannot directly understand that this is a "keyword list". However, don't worry, AnQiCMS also providessplita filter to solve this problem.
splitThe filter can split a string into an array according to the specified delimiter. Then, we can pass the split array tocountFilter statistics.
Usage:
splitThe basic usage of the filter is as follows:
{{ obj|split:分隔符 }}
Comprehensive Example:
Assuming your article tags (tag) are stored as a comma-separated string inarchive.Tagsvariable, you want to count the number of times "Go language" appears:
{% 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 in the article tags.
Even if the original data format does not fully match, such a combination can be used.countThe array form processed directly by the filter, we can also implement keyword statistics in simple steps.
Actual application scenarios
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 content, to assist the content team in adjusting SEO strategies.
- Content AnalysisUnderstand the frequency of mention of a specific word in user-defined fields (such as product features, service advantages, etc.) to better understand the content focus.
- Dynamic content display:Based on the frequency of certain keyword occurrences, dynamically adjust the display strategies of related recommendations and advertising positions on the page to enhance user experience and content value.
The flexibility of AnQiCMS template engine allows you to easily manage these seemingly complex content operation requirements, transforming technical information into practical display effects.
Common Questions (FAQ)
Q1:countIs the filter case-sensitive when counting?
A1: Yes,countThe filter is case-sensitive when performing keyword matching.For example, counting "CMS" and counting "cms" will yield different results.upperorlowerThe filter will convert all content in the target string or array to uppercase or lowercase before counting.
For example:{{ description|lower|count:"cms" }}.
Q2: How can I count the number of occurrences of keywords in the article content (HTML output from the rich text editor)?
A2: If the content of the article is edited through a rich text editor, it will usually contain HTML tags. It is usually not recommended to usecountFilter may count text within HTML tags, or may lead to inaccurate counting. To accurately count pure text keywords, it is recommended that you use first.striptagsFilter removes all HTML tags and then usescountFilter.
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, rather than how many times it appears, which filter should I use?
A3: If you are only concerned about whether the keyword exists, not the specific number of occurrences, you can usecontaina filter. It will return a boolean valueTrueorFalse),indicating whether the keyword exists in a string or array.
For example:
{% set text = "AnQiCMS让您的网站更安全" %}
{% if text|contain:"安全" %}
<span>内容包含“安全”关键词。</span>
{% endif %}