In the template design of AnQi CMS, we may sometimes need to perform more detailed analysis and display of content, such as finding the first occurrence position of a specific keyword in a text, or counting how many times it appears.These requirements are very useful in dynamic content display, information extraction, or auxiliary SEO.Benefiting from the CMS adopting a template engine syntax similar to Django, we can utilize its powerful filtering functions to achieve these goals.
接下来,我们将探讨如何在English CMS模板中,利用内置的indexandcount过滤器,高效地完成这些操作。
查找关键词的首次出现位置:indexFilter
In the template of AnQi CMS, when we encounter the need to locate the first occurrence of a specific keyword in a segment of text,indexFilter is your powerful assistant. This filter will return the starting index of the first occurrence of the keyword in the string.
Usage:
indexThe basic syntax of the filter is very intuitive:
{{ 原始字符串变量|index:关键词 }}
Among them:
原始字符串变量It is the text content you want to search.关键词Is the substring you want to search for.
Interpretation of the returned results:
- If the keyword is found in the original string,
indexThe filter returns an integer representing the starting position of the first occurrence of the keyword. This index position is from0. - If the keyword is not found in the original string,
indexthe filter will return-1. - It should be particularly noted that when dealing with strings containing Chinese characters,
indexThe filter considers a Chinese character to occupy 3 positions when calculating the position. For example, in the Chinese sentence "你好3.
Actual application example:
Assume we have an article titlearticle.TitleThe content is "Welcome to AnQiCMS (AnQiCMS)", we want to know the first occurrence position of "CMS":
{% set articleTitle = "欢迎使用安企CMS(AnQiCMS)" %}
{% set firstIndex = articleTitle|index:"CMS" %}
<p>标题: {{ articleTitle }}</p>
<p>"CMS" 首次出现的位置在索引: {{ firstIndex }}</p>
{% if firstIndex != -1 %}
<p>关键词已找到!</p>
{% else %}
<p>关键词未找到。</p>
{% endif %}
The code will output:
标题: 欢迎使用安企CMS(AnQiCMS)
"CMS" 首次出现的位置在索引: 18
关键词已找到!
By usingindexThe result of the filter is-1compared, we can easily determine if the keyword exists and display different content as needed.
Count the number of times the keyword appears:countFilter
If your requirement is to count the total number of times a keyword repeats in a string,countThe filter will perfectly meet your requirements. It will traverse the entire string and calculate the frequency of the specified keyword.
Usage:
countThe syntax of the filter is also concise and clear:
{{ 原始字符串变量|count:关键词 }}
Among them:
原始字符串变量It is the text content you want to search.关键词It is the substring you want to count the occurrences of.
Interpretation of the returned results:
countThe filter will return an integer representing the total number of times the keyword appears in the original string.- If the keyword is not found, it will return
0.
Actual application example:
Continue using the article title mentioned earlier: ”Welcome to AnQiCMS (AnQiCMS)”, let's count how many times “CMS” appears:
{% set articleTitle = "欢迎使用安企CMS(AnQiCMS)" %}
{% set occurrenceCount = articleTitle|count:"CMS" %}
<p>标题: {{ articleTitle }}</p>
<p>"CMS" 在标题中出现了: {{ occurrenceCount }} 次</p>
{% if occurrenceCount > 0 %}
<p>关键词多次出现,可能是一个重要主题。</p>
{% else %}
<p>关键词未在标题中出现。</p>
{% endif %}
The code will output:
标题: 欢迎使用安企CMS(AnQiCMS)
"CMS" 在标题中出现了: 2 次
关键词多次出现,可能是一个重要主题。
This feature is very useful in analyzing content density, detecting repeated words, or dynamically adjusting content display based on the frequency of specific keywords.
Advanced Application and Precautions
Combined with other filters.
indexandcountFilter not only applies to simple strings, but can also be used withsplitCombining filters, processing array (or called slice/slice) type data. For example, split a comma-separated string into an array first, and then find a specific element in the array:
{% set tagsString = "安企CMS,CMS模板,建站系统,CMS" %}
{% set tagsArray = tagsString|split:"," %}
{% set cmsCountInArray = tagsArray|count:"CMS" %}
{% set cmsIndexInArray = tagsArray|index:"CMS模板" %}
<p>标签列表: {{ tagsString }}</p>
<p>"CMS" 在数组中出现了: {{ cmsCountInArray }} 次 (注意:这里是完全匹配数组元素)</p>
<p>"CMS模板" 在数组中首次出现的位置: {{ cmsIndexInArray }}</p>
case-sensitive
Please note,indexandcountThe filter is used to match keywords whencase-sensitiveEnglish.This means that “cms” and “CMS” are considered as different keywords.lowerorupper), and then perform operations.
{% set text = "AnQiCMS是一款优秀的CMS" %}
{% set keywordLower = "cms" %}
<p>原始文本: {{ text }}</p>
<p>查找 "cms" (区分大小写): {{ text|count:keywordLower }} 次</p>
<p>查找 "cms" (不区分大小写): {{ text|lower|count:keywordLower }} 次</p>
It is applicable to different data types
- String:
indexandcountIt will search for a substring in the string content. - Array/Slice:
indexandcountIt will check if there exists a keyword in the arrayExact matchelement, and return its index or count. They do not perform substring matching. - For structured data such as objects or maps, if you need to determine whether a certain key name exists, you can use
containa filter, but it does not directly provide indices or counts.
Summary
In the templates of AnQi CMS,indexandcountThe filter provides us with powerful string analysis capabilities, whether you want to accurately locate keywords or count their frequency in the text, it can be easily achieved.Combine condition judgment and loop template tags, these filters can help you build more intelligent and dynamic website content display logic.When in actual use, please pay attention to their case sensitivity features and matching behaviors under different data types to ensure the expected effect.
Common Questions (FAQ)
Q1:indexandcountDoes the filter distinguish between uppercase and lowercase?
A1:Yes,indexandcountThe filter is case-sensitive when searching for keywords.For example, searching for 'cms' and 'CMS' will yield different results.lower), and then perform operations.
Q2:indexWhat do the numbers returned by the filter represent? What if the keyword is not found?
A2: indexThe filter returns the starting position (index) of the first occurrence of the keyword in the string. This index is from0Starting the calculation. For example, if the keyword is the first character of the string, then its index is0If the keyword is not found,indexit will return-1.
Q3: Can these filters be used for other data types besides strings?
A3: countandindexThe filter is mainly used for strings and arrays (or also known as slices/slice). For arrays, they check if the keywords are ascomplete elementsContains within the array, rather than a partial match. If you need to determine whether an object or map contains a specific key name,containFilter, but it does not provide index or count statistics.