In the template development of AnQi CMS, we often need to finely control the displayed content, which includes flexible handling of string and array content.Understand and make good use of the various template filters provided by the AnQi CMS, which can greatly enhance our ability to build dynamic and intelligent websites.indexIt can help us accurately locate the first occurrence of a keyword in a string or array.

indexFilter: The tool for accurately locating keywords.

Imagine that you may need to adjust the display style based on whether the content of the article contains specific sensitive words, or when handling a dynamically generated list of data, you need to know whether a specific value exists in the list, or even its specific position in the list.indexThe filter is exactly for these scenarios, it can help us find the starting position of the first occurrence of the specified keyword in the target string or array.

indexThe working principle of the filter

indexFilter takes two parameters: one is theEnglish(which can be a string or an array), and the other isKeywords(the content you want to search for). Its return result is an integer:

  • If the keyword is found, it will return the starting position of the first occurrence of the keyword (counting from 0).
  • If the keyword is not found, it will return-1.

How to useindexFilter

indexThe basic usage syntax of the filter is very intuitive:

{{ obj|index:关键词 }}

Here are theobjIt can be a string containing text, or an array (slice) that stores multiple values.关键词is what you want to be inobjthe specific text or numeric value you are looking for.

Example one: Find keywords in a string

We have a welcome string: "Welcome to AnQiCMS (AnQiCMS)

{{"欢迎使用安企CMS(AnQiCMS)"|index:"CMS"}}

Run this template code and the output will be18.

Example two: Find an element in an array

indexThe filter also applies to arrays. Suppose we have a throughfieldsFilter the array split from the string:["splits", "the", "string", "安企CMS"]We want to find the position of the element 'the'.

{% set values = "splits the string 安企CMS"|fields %}
{{values|index:"the"}}

This code will output1In the array, the position also starts from 0, so "splits" is at position 0, and "the" is at position 1. It should be noted that when searching in the array, auto .indexFilter requires that the keyword matches the elements in the array exactly.

Example three: The keyword is not found.

IfindexFilter failed to find the specified keyword in the target, it will return a special value-1This is very useful in conditional judgments, such as:

{% set content = "这是一个关于网站运营的文章" %}
{% set position = content|index:"安企CMS" %}

{% if position != -1 %}
    <p>内容中包含了“安企CMS”,位置在:{{ position }}</p>
{% else %}
    <p>内容中没有找到“安企CMS”。</p>
{% endif %}

This code will output 'The content does not contain 'AnQi CMS' because 'AnQi CMS' does not appear in the given string.'

UseindexImportant considerations for filters

  • The position of the first occurrence: indexThe filter will only return the position of the first occurrence of the keyword. If the same keyword appears multiple times in a string or array, it will not return all positions.
  • As explained in the string example, the special processing of Chinese characters:is as follows, a Chinese character in the string example:indexThe filter calculation position is considered to occupy 3 bytes of space. This is a detail that template developers for Chinese websites need to pay special attention to.
  • Exact match in the array:When usingindexThe filter finds array elements by requiring the keyword to match some element in the array completely. Partial matching is invalid.
  • Case sensitive: indexThe filter is case-sensitive when searching for keywords. For example, searching for 'CMS' and 'cms' will yield different results.

Summary

indexThe filter is a powerful and flexible tool in the Anqi CMS template, which helps us accurately locate the first occurrence of keywords, whether we are dealing with strings or arrays.By understanding its working principle and usage注意事项, we can write more intelligent and responsive templates, thus better controlling the display and interaction logic of website content.


Common Questions (FAQ)

1.indexFilter is case sensitive?

Yes,index

2. BesidesindexFilter, what other features does the security CMS have that can determine if a text contains a specific keyword?

If you just want to determine whether a string or an array contains a certain keyword without needing to know the specific position, you can usecontainFilter. It will directly return a boolean value (TrueorFalse), it is more concise to use, for example:{{"欢迎使用安企CMS"|contain:"安企CMS"}}.

3. Why is the position of 'CMS' in the Chinese '安企CMS' 18 instead of other numbers?

This is because the Anqi CMS is developed based on the Go language, and the Go language will perform byte calculations based on UTF-8 encoding when handling strings.An English character (such as "joyindexThe filter returns the starting byte position of the keyword in the string, rather than the character position that we usually understand visually.When performing string truncation or positioning, the byte length needs to be taken into account.