In Anqi CMS template design, sometimes we may need to analyze and display content more finely, such as finding the position of a specific keyword for the first time in a text or counting how many times it appears.These requirements are very useful in dynamic content display, information extraction, or辅助SEO.Benefiting from AnQi CMS adopting a template engine syntax similar to Django, we can take advantage of its powerful filter functions to achieve these goals.
Next, we will discuss how to use the built-inindexandcountfilter, efficiently complete these operations.
to find the first occurrence of a keyword:indexFilter
In AnQi CMS template, when we encounter the need to locate the first occurrence of a specific keyword in a text,indexThe filter is your reliable 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 for.关键词Is the substring you want to search for.
Interpretation of the returned result:
- 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 position index is from0the calculation starts. - If the keyword is not found in the original string,
indexThe filter will return-1. - It should be noted that when processing strings containing Chinese,
indexThe filter considers a Chinese character to occupy 3 positions when calculating positions. For example, in the Chinese sentence '你好', the index position of '好' will be3.
Actual application example:
Assuming we have an article titlearticle.TitleThe content is "Welcome to AnQiCMS (AnQiCMS)". We want to know the position of the first occurrence 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 with-1Comparing, 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, thencountThe filter will perfectly meet your requirements. It will traverse the entire string, calculating the frequency of the specified keyword.
Usage:
countThe syntax of the filter is also simple and clear:
{{ 原始字符串变量|count:关键词 }}
Among them:
原始字符串变量It is the text content you want to search for.关键词Is the substring you want to count the occurrence of.
Interpretation of the returned result:
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 you just used: '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, and other aspects.
Advanced Applications and Precautions
Use with other filters
indexandcountThe filter is not only suitable for simple strings, but can also be used withsplitCombine filters to process array (also known as slice/slice) data. For example, first split a comma-separated string into an array, and then search for 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 when performing keyword matchingCase sensitiveThis means that 'cms' and 'CMS' will be considered as different keywords.If you need to perform a case-insensitive search, you can consider converting both the original string and the search keyword to uppercase or lowercase (for example, usinglowerorupperThe filter), then perform the operation.
{% set text = "AnQiCMS是一款优秀的CMS" %}
{% set keywordLower = "cms" %}
<p>原始文本: {{ text }}</p>
<p>查找 "cms" (区分大小写): {{ text|count:keywordLower }} 次</p>
<p>查找 "cms" (不区分大小写): {{ text|lower|count:keywordLower }} 次</p>
Applicable to different data types
- String:
indexandcountIt will find the substring in the string content. - Array/Slice:
indexandcountChecks if the array contains an element with the keywordPerfect matchand returns 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 key name exists, you can use
containa filter, but it does not directly provide an index or count.
Summary
In the AnQi CMS template,indexandcountThe filter provides us with powerful string analysis capabilities, whether you want to precisely locate keywords or count their frequency in the text, it can be easily achieved.Combine conditional judgment and loop template tags, these filters can help you build a more intelligent and dynamic website content display logic.In actual use, please pay attention to their case sensitivity and matching behavior under different data types to ensure the expected effect.
Frequently Asked Questions (FAQ)
Q1:indexandcountIs the filter case sensitive?
A1:Yes,indexandcountThe filter is case-sensitive when searching for keywords. For example, searching for 'cms' and 'CMS' will yield different results.If you need to perform a case-insensitive search, you can first convert both the original string and the search keyword to uppercase or lowercase (for example, usinglowerThe filter), then perform the operation.
Q2:indexWhat does the number returned by the filter represent? What happens 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 from0Start calculating. For example, if the keyword is the first character of the string, then its index is0. If the keyword is not found,indexwill 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 what is called slices/slice). For arrays, they check if the keywords are ascomplete elementsexists within the array, not a partial match. If you need to determine whether a key name exists in an object or a mapping (map), you can consider usingcontainA filter that does not directly provide index or count statistics.