In AnQi CMS template development, we often need to finely control the content displayed, which includes flexible handling of string and array content.Understand and make good use of the various template filters provided by Anqi CMS, which can greatly enhance our ability to build dynamic and intelligent websites.Today, let's delve deeply into a very practical filter -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 might need to adjust the display style based on whether the content of the article contains specific sensitive words, or when handling dynamically generated data lists, you need to know whether a specific value exists in the list, or even know its specific position in the list.indexThe filter is designed for these scenarios, it can help us find the starting position of the first occurrence of a specified keyword in a target string or array.

indexThe principle of the filter.

indexThe filter takes two parameters: one is the one to be searched forthe target(which can be a string or an array), and the other isKeywords(the content you want to find). 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:关键词 }}

HereobjIt can be a string containing text or an array (slice) that stores multiple values.关键词That is what you want to beobjsearched for the specific text or numeric value.

Example 1: Find a keyword in a string

Assuming we have a welcome string: 'Welcome to AnQiCMS (AnQiCMS)', we want to know where the word 'CMS' first appears.

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

Run this template code and the output will be18.

You might be curious, why not 6 or another number?This is because at the bottom of AnQi CMS, string processing is based on UTF-8 encoding byte calculations.A Chinese character usually occupies 3 bytes. Therefore, the five characters in "Welcome to AnQi" occupy 15 bytes, plus 1 byte for a space, followed by "CMS", so its starting byte position is 15 (Welcome to AnQi) + 3 (space) = 18.This is very important for accurately calculating the position of Chinese content.

Example two: Find an element in an array

indexThe filter also applies to arrays. Suppose we have a throughfieldsThe filter splits the string into an array:["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 output1. In an array, the position also starts from 0, so 'splits' is at position 0, 'the' is at position 1. It is important to note that when searching in an array, }indexThe filter requires that the keyword matches the elements in the array exactly.

Example three: The keyword is not found.

IfindexThe filter could not find the specified keyword in the target, it will return a special value-1This is very useful in conditional judgments, for example:

{% 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 the filter

  • The first occurrence position: indexThe filter will only return the first occurrence position of the keyword. If the same keyword appears multiple times in a string or array, it will not return all positions.
  • Special processing of Chinese characters:As explained in the string example, a Chinese character is inindexThe filter calculates the position and 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:While usingindexThe filter searches for array elements and requires the keyword to match exactly with some element in the array. Partial matches are 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 can help us accurately locate the first occurrence of a keyword, whether it is dealing with strings or arrays.By understanding its working principles and usage precautions, we can write more intelligent and responsive templates to better control the display and interactive logic of website content.


Frequently Asked Questions (FAQ)

1.indexIs the filter case-sensitive?

Yes,indexThe filter is case-sensitive when searching for keywords. For example, searching for "cms" in the string "AnQiCMS" will return -1 because it cannot find an exact match for "cms".You need to make sure that the search keywords match the case of the text in the target.

2. BesidesindexFilter, what similar features does the Anqi CMS have to determine if text contains a keyword?

If you just want to check if a string or array contains a certain keyword without needing to know the specific position, you can usecontaina filter. It will directly return a boolean value (TrueorFalse),Use it more concisely, for example:{{"欢迎使用安企CMS"|contain:"安企CMS"}}.

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

This is because Anqi CMS is developed based on Go language, and Go language will perform byte calculation based on UTF-8 encoding when processing strings.A Chinese character (such as '欢', '迎' etc.) usually occupies 3 bytes. Therefore,indexThe filter returns the starting byte position of the keyword in the string, not the character position that we usually understand visually.When truncating or locating a string, you need to consider the length of this byte.