In the flexible and powerful template system of AnQiCMS, we often need to process and judge various text content on the page. Among them,indexThe filter is a very practical tool that helps us quickly locate the first occurrence of a keyword in a string or array.
So, when the keyword we need to search for is not present at all in the target string or array,indexThe filter will return what result to indicate this situation? The answer is: it will return a clear-1.
In the conventions of programming and template processing, when a search operation fails to find the target, it returns-1It is a common and clear signal indicating "not found" or "does not exist". AnQiCMS'sindexThe filter also adheres to this standard, allowing you to easily identify and handle missing keywords when building dynamic content.
ThisindexThe filter is not only applicable to ordinary text strings, but also can find the specified value in an array (Go language slice).When you use it to handle various content, such as checking if an article title contains a specific word or determining if a tag array has a specific tag name, it can be very useful.
It is worth noting that if the string you are processing contains Chinese,indexThe filter will treat each Chinese character as occupying 3 positions when calculating the position. For example, when searching for "world" in the string "Hello world", it will return6(Because "you" takes up 3 characters, "good" takes up 3 characters, "world" starts from the 6th character). But regardless of how character encoding calculates position, it always returns if not found.-1The rules will not change.
Let's look at some practical examples.indexHow the filter works:
Example one: the keyword exists in the string.
Assuming we have the title of an article and we want to find if it contains the word "CMS":
{% set pageTitle = "欢迎使用安企CMS(AnQiCMS)" %}
{% set position = pageTitle|index:"CMS" %}
<p>"{{ pageTitle }}" 中 "CMS" 的首次出现位置是:{{ position }}</p>
After running, you will see output similar to this:"欢迎使用安企CMS(AnQiCMS)" 中 "CMS" 的首次出现位置是:18This indicates that the "CMS" starts at the 18th position in the string (counting from 0).
Example two: The keyword does not exist in the string.
Now, we try to find a non-existent keyword in a title:
{% set pageTitle = "欢迎使用安企CMS(AnQiCMS)" %}
{% set position = pageTitle|index:"模板制作" %}
<p>"{{ pageTitle }}" 中 "模板制作" 未找到时的结果是:{{ position }}</p>
This time, since the word 'template creation' is not in the title, you will get this result:"欢迎使用安企CMS(AnQiCMS)" 中 "模板制作" 未找到时的结果是:-1
Example 3: Find keywords in an array
indexFilters also apply to arrays. Suppose we have a list of category names:
{% set categories = ["网站建设", "内容管理系统", "SEO优化"] %}
{% set position = categories|index:"免费建站" %}
<p>在分类列表中,"免费建站" 未找到时的结果是:{{ position }}</p>
Due to the 'Free Website' not being in our category list, the result would be the same:在分类列表中,"免费建站" 未找到时的结果是:-1
Example four: using conditional logic
It is becauseindexThe explicit one returned by the filter-1We can easily write conditional logic in the template. You can judge whether a variable contains a certain keyword and decide whether to display certain content, adjust the style, or trigger a specific feature display accordingly.
{% set articleContent = "AnQiCMS 支持灵活的内容模型和自定义字段。" %}
{% set searchKeyword = "自定义" %}
{% set foundPosition = articleContent|index:searchKeyword %}
{% if foundPosition != -1 %}
<p>文章内容中包含关键词 "{{ searchKeyword }}",首次出现位置是 {{ foundPosition }}。</p>
{% else %}
<p>文章内容中不包含关键词 "{{ searchKeyword }}"。</p>
{% endif %}
{% set anotherSearchKeyword = "多语言" %}
{% set anotherFoundPosition = articleContent|index:anotherSearchKeyword %}
{% if anotherFoundPosition != -1 %}
<p>文章内容中包含关键词 "{{ anotherSearchKeyword }}",首次出现位置是 {{ anotherFoundPosition }}。</p>
{% else %}
<p>文章内容中不包含关键词 "{{ anotherSearchKeyword }}"。</p>
{% endif %}
This code will output based on whether the keyword exists:文章内容中包含关键词 "自定义",首次出现位置是 18。
文章内容中不包含关键词 "多语言"。
In summary, when you use AnQiCMS'sindexThe filter searches for a keyword within a string or array, and when the keyword does not actually exist, you get a clear-1This is the key value that you build smart, dynamic content in the template, allowing you to better control the display logic of the page.
Frequently Asked Questions (FAQ)
1. In addition toindexFilter, does AnQiCMS have other methods to determine if a string contains a certain keyword?Of course. You can consider usingcontainfilter.containThe filter directly returns a boolean value (TrueorFalseIt indicates whether the keyword exists without needing to judge whether the returned number is-1It may be more intuitive to use in some scenarios.
2. If I want to know how many times a keyword appears in a string rather than the position of its first occurrence, which filter should I use?For the need of counting keyword occurrences, AnQiCMS providedcountA filter that accurately calculates the total number of times a keyword appears in a string or array on a line, rather than just giving the position of the first occurrence.
3.indexDo you need to pay attention to anything else when the filter processes Chinese characters, besides the different position calculation method?Yes, mainly it is the difference in the way of position calculation. Because Chinese characters may occupy multiple bytes in the underlying encoding,indexThe filter reflects this underlying byte position when returning the location, that is, a Chinese character is counted as 3 positions. However, regardless of the character type, when the keyword does not exist,indexThe filter will always return-1This is always true, you can safely use this result for logical judgment.