In the flexible and powerful template system of AnQiCMS, we often need to process and judge various text content on the page.indexA filter is a very practical tool that helps us quickly locate the first occurrence of a keyword in a string or array.

Then, when the keyword we need to search for does not exist at all in the target string or array,indexWhat result does the filter return to indicate this situation? The answer is: it returns 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 can also search for specified values in arrays (slices in Go language).When you use it to handle various content, such as checking whether the title of an article contains a specific word, or determining whether a tag array has a specific tag name, it can come in handy.

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 "你好世界", it will return6(Because "you-1The rules will not change.

Let's look at several practical examples to seeindexHow the filter works:

Example 1: The keyword exists in the string

Assuming we have an article title and we want to check 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 an output similar to this:"欢迎使用安企CMS(AnQiCMS)" 中 "CMS" 的首次出现位置是:18This indicates that the 'CMS' appears at the 18th position in the string (starting 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 three: Finding 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>

Since 'Free Website Building' is not in our category list, the result will be the same:在分类列表中,"免费建站" 未找到时的结果是:-1

Example Four: Using Conditional Logic

It is precisely becauseindexThis explicit filter returns-1We can easily write conditional logic in the template.You can determine if a variable contains a certain keyword and decide whether to display some content, adjust styles, or trigger specific feature displays 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 according to whether the keyword exists:文章内容中包含关键词 "自定义",首次出现位置是 18。 文章内容中不包含关键词 "多语言"。

In summary, when you use AnQiCMS,indexThe filter looks for a specific keyword in a string or array, and when the keyword does not actually exist, you will get a clear-1This return value is the key to building intelligent and dynamic content in your template, allowing you to better control the display logic of the page.


Common Questions and Answers (FAQ)

1. BesidesindexFilter, does AnQiCMS have other methods to determine if a string contains a specific keyword?Of course. You can consider usingcontainFilter.containThe filter returns a boolean value (TrueorFalse),represents whether the keyword exists, without the need for you to judge whether the returned number is-1,which may be more intuitive 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 to count the frequency of keywords, AnQiCMS providescountFilter. It can accurately calculate the total number of times a keyword appears in a line of string or array, rather than just giving the first occurrence position.

3.indexIs there anything else to pay attention to when the filter processes Chinese characters, besides the different position calculation method?Yes, mainly the difference in the way the location is calculated. Since Chinese characters may occupy multiple bytes in the underlying encoding,indexThe filter reflects this underlying byte position when returning the position, 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-1, this is not changeable, you can safely use this result for logical judgment.