As Anqicms users, we often need to manage and analyze website content in a refined manner, one of the common requirements is to count the frequency of specific keywords in article titles or descriptions.This not only helps us understand the effectiveness of content marketing, optimize SEO strategies, but also better grasp user focus points.AnQi CMS, with its flexible template engine and rich built-in filters, makes this operation feasible at the front-end template level.
We need to utilize the powerful functions of the Anqi CMS template system, especially its 'filter' mechanism, in order to count the number of times a specific keyword appears in the article title or description.The template language of AnQi CMS is similar to Django, by cleverly combining tags and filters, we can dynamically calculate and display these data when the page is loaded.
Understanding Anqi CMS templates and content data
In Anqi CMS, articles and their related information (such as titles, descriptions) are usually througharchiveListorarchiveDetailTags to retrieve. These tags will return one or more article objects, each of which includes various properties of the article, such asTitle(Article Title) andDescription(Article description). Our statistical work will focus on these properties.
The Anqi CMS template engine provides a variety of built-in filters that can perform various operations on variables, including string operations and format conversions. Among them,countThe filter is the core tool we use to implement keyword statistics.It can calculate the number of times a substring appears in a line of text, or the number of times an element appears in an array.
UsecountFilter keyword statistics
Let us take the example of counting the frequency of the keyword "AnQi CMS" in the article title and description. Suppose we are editing a template file for an article list page (such aslist.htmlorcategory.htmlOr a detailed article page (for example)detail.html)
Firstly, we can define a variable to store the keywords we want to count, which is convenient for management and modification:
{% set target_keyword = "安企CMS" %}
Next, when we iterate over the article list, we can apply the filter to the title and description of each articlecountfilter.
{% archiveList articles with type="page" limit="10" %}
{% for article in articles %}
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
{% set title_occurrences = article.Title|count:target_keyword %}
{% if title_occurrences > 0 %}
<p>关键词 "{{ target_keyword }}" 在标题中出现 {{ title_occurrences }} 次。</p>
{% endif %}
<p>描述:{{ article.Description }}</p>
{% set description_occurrences = article.Description|count:target_keyword %}
{% if description_occurrences > 0 %}
<p>关键词 "{{ target_keyword }}" 在描述中出现 {{ description_occurrences }} 次。</p>
{% endif %}
<hr>
{% empty %}
<p>暂无文章。</p>
{% endfor %}
{% pagination pages with show="5" %}
{# 这里是分页导航的代码 #}
{% endpagination %}
{% endarchiveList %}
In this code block:
- We first use
archiveListTag to get the article list. - In
forthe loop,article.Titleandarticle.DescriptionRepresent the title and description of the current article. |count:target_keywordThe filter will calculatetarget_keywordInarticle.Titleorarticle.DescriptionThe number of times a string appears as a substring in the string, and assign the result totitle_occurrencesordescription_occurrencesVariable.- We use
{% if ... %}Logical judgment tag, only when the keyword appears more than 0 times, will the statistical results be displayed on the page, maintaining the page's cleanliness.
Advanced Application: Handling Case-Insensitive and More Precise Word Statistics
The AbovecountThe filter is case-sensitive by default, which means that "CMS" and "cms" are considered different keywords. If we need to perform case-insensitive statistics, we can cleverly combinelowerThe filter, which converts the article title/description and target keywords to lowercase, and then performs the count:
{% set target_keyword = "anqicms" %} {# 目标关键词统一使用小写 #}
{% archiveList articles with type="page" limit="10" %}
{% for article in articles %}
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
{% set article_title_lower = article.Title|lower %} {# 将标题转为小写 #}
{% set title_occurrences_insensitive = article_title_lower|count:target_keyword %}
{% if title_occurrences_insensitive > 0 %}
<p>关键词 "{{ target_keyword }}" (不区分大小写) 在标题中出现 {{ title_occurrences_insensitive }} 次。</p>
{% endif %}
{# 描述部分同理 #}
<hr>
{% endfor %}
{% endarchiveList %}
If you want to implement a more strict 'complete word' count (instead of substrings), for example, 'CMS' should only count the independent 'CMS' words, not the 'CMS' part in 'AnQiCMS', which would be slightly more complex in the template layer of AnQiCMS because the template filter does not directly support complex regular expressions. One workaround is to first usesplitThe filter splits the title or description into an array of words, then traverses the array for exact matching and counting. However, this significantly increases the logical complexity and computational volume of the template. In most cases, for SEO keyword density analysis, the substring'scountIt is already sufficient to meet the needs.
How to operate in Anqi CMS
- Log in to the backend:Enter your Anqi CMS management background.
- Navigate to template design:On the left