In the template development of AnQi CMS, mastering various filters can make the display of page content more flexible and efficient. Among them,indexThe filter is a common tool used for array or string search tasks. However, many users may have a question when first encountering it: when searching for a specific value in an array,indexDoes the filter return the index position of this value or some other identifier?

The answer is clear: of the Anqi CMSindexThe filter returns the specific value when searching for it in an array or stringThe index position of the first occurrence of the number. If the value to be searched for does not exist, it will return-1This behavior is similar to the search functions of arrays or strings in many programming languages, providing a straightforward numerical index to represent the position of an element.

Deep understandingindexFilter

indexThe filter is mainly used to determine whether a target (string or array/slice) contains a specific keyword and to provide its position information.The principle of its operation is to find the first occurrence of the keyword.

  1. Search ObjectIt can act on strings (searching for substrings) and arrays/slices (searching for elements).
  2. Return value:
    • If the keyword is found, returns a non-negative integer representing the first occurrence.First occurrenceThe starting index position. The index is from0.
    • If the keyword is not found, return-1.
  3. special case:
    • when searching within a string, if the string containsChineseA Chinese character is considered to occupy3 positionsThis usually relates to the byte length of UTF-8 encoding.This needs to be paid special attention when in use, as it may differ from our intuitive feeling of 'one character occupies one position' that we are accustomed to.

indexHow to use the filter

indexThe basic syntax of the filter is very concise:

{{ obj|index:关键词 }}

Here:

  • objis the target you want to search for, which can be a string variable, or an array (or slice) variable.
  • 关键词in which you want to search.objfor the specific content.

Actual application example

We go through several examples to see in detailindexHow filters work.

Example one: Find in a string

Suppose we have a stringmessage = "欢迎使用安企CMS(AnQiCMS)".

{% set message = "欢迎使用安企CMS(AnQiCMS)" %}

<div>查找“CMS”的位置:{{ message|index:"CMS" }}</div>
<div>查找“欢迎”的位置:{{ message|index:"欢迎" }}</div>
<div>查找“世界”的位置:{{ message|index:"世界" }}</div>
<div>查找“安企”的位置(注意中文长度):{{ message|index:"安企" }}</div>

The running result will be:

查找“CMS”的位置:18
查找“欢迎”的位置:0
查找“世界”的位置:-1
查找“安企”的位置(注意中文长度):0

Here, the Chinese characters '欢迎' occupy positions 0-5 of the six byte positions, and the Chinese characters '安企' also occupy positions 0-5 of the six byte positions, so the index of '安企' is still 0.The index of the first 'C' is 18 because the six Chinese characters '欢迎使用安企' occupy 18 bytes in length (6 characters * 3 bytes/character).

Example two: Find in an array

Now we create a string array and find elements in it. We can usesplita filter to split a string into an array.

{% set features = "安企CMS,AnQiCMS,内容管理系统,免费建站系统,免费模板"|split:"," %}

<div>在数组中查找“内容管理系统”的位置:{{ features|index:"内容管理系统" }}</div>
<div>在数组中查找“高级SEO”的位置:{{ features|index:"高级SEO" }}</div>

{% set searchKeyword = "免费模板" %}
<div>在数组中查找“{{ searchKeyword }}”的位置:{{ features|index:searchKeyword }}</div>

The running result will be:

在数组中查找“内容管理系统”的位置:2
在数组中查找“高级SEO”的位置:-1
在数组中查找“免费模板”的位置:4

When searching for elements in an array,indexThe filter performs exact matching and does not consider substrings like string search.For example, if you search for 'management', it cannot be found in the array containing 'Content Management System', because it is not an independent element.

Example Three: Using Conditional Judgement

indexThe numeric results returned by the filter, especially suitable for use withifLogical judgment tags to achieve more complex template logic.

{% set userRole = ["admin", "editor", "viewer"] %}
{% set currentUser = "editor" %}

{% if userRole|index:currentUser != -1 %}
    <p>当前用户 "{{ currentUser }}" 拥有 "{{ currentUser }}" 角色。</p>
{% else %}
    <p>当前用户 "{{ currentUser }}" 没有找到相应的角色。</p>
{% endif %}

{% set guestUser = "guest" %}
{% if userRole|index:guestUser != -1 %}
    <p>当前用户 "{{ guestUser }}" 拥有 "{{ guestUser }}" 角色。</p>
{% else %}
    <p>当前用户 "{{ guestUser }}" 没有找到相应的角色。</p>
{% endif %}

The running result will be:

<p>当前用户 "editor" 拥有 "editor" 角色。</p>
<p>当前用户 "guest" 没有找到相应的角色。</p>

By checkingindexWhether the result of the filter is not equal-1We can easily determine whether a value exists in the target and render different content blocks accordingly.

Summary

Anqi CMS'sindexA filter is a functional and practical tool that is used to find the first occurrence of a specific value in a string or array and return its numeric index position, if not found then it returns-1Understanding its working principle, especially the counting rules of Chinese characters in string search, can help us build template logic more accurately, thereby efficiently managing and displaying website content.


Common Questions (FAQ)

How to determine if a value exists in an array or a string without concerning its specific position?

If you only need to check if a value exists without knowing its specific location, you can usecontainFilter.containThe filter returns a boolean value (TrueorFalseThis indicates whether the value is included, which is more concise and clear in scenarios where only existence checking is needed.

2.indexCan the filter search for subarrays or patterns (regular expressions) within strings?

indexThe filter performs direct value or substring matching, and does not support searching within arrays.For strings, it only searches for exact matching substrings, and does not support regex pattern matching.If more complex pattern matching is required, it may be necessary to handle it in the business logic layer and pass the processed data to the template.

What if I need to get the index positions of all matches?indexCan the filter do that?

indexThe filter only returns the keywordFirst occurrenceThe index position.It cannot directly obtain the index positions of all matching items.To implement this feature, you may need to perform more complex logic processing in Go language code outside the template, or preprocess the data into a structure containing all indices before passing it to the template.On the template level, it focuses on the efficiency and conciseness of the first search.