What result does the `index` filter return when the keyword does not exist in the string?

Calendar 👁️ 72

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.

Related articles

The `index` filter returns the index position or other identifier when searching for a specific value in an array?

In Anqi CMS template development, proficiently using various filters can make the display of page content more flexible and efficient.Among them, the `index` filter is a commonly used tool for handling array or string search tasks.However, many users may have a question when they first contact: When searching for a specific value in an array, does the `index` filter return the index position of the value or some other identifier?

2025-11-07

How to get the position of the first occurrence of a keyword in the AnQiCMS article title?

In website content operations, we sometimes need to refine the processing of article titles, such as highlighting a keyword or organizing content based on the position of the keyword.In terms of AnQiCMS, the system provides a powerful template engine, combined with its built-in filters, we can easily achieve these requirements.Today, let's discuss how to accurately obtain the position of the first occurrence of a keyword in the article title of AnQiCMS.### Core Function Unveiling: `index` Filter It can be used in AnQiCMS

2025-11-07

How does the `count` filter handle the counting logic for Chinese character strings?

In daily content operations, we often encounter scenarios where we need to perform statistical analysis on content, such as counting the number of times specific keywords appear, checking the frequency of repetition of certain elements in the content, etc.AnQiCMS provides many practical template filters to help us achieve these functions, where the `count` filter is a powerful tool for counting.When dealing with content containing Chinese characters, it is particularly important to understand how the `count` filter performs counting.

2025-11-07

How to use the `count` filter to analyze the keyword density on AnQiCMS templates?

In website operation and Search Engine Optimization (SEO), keyword density is a common but easily misunderstood concept.It refers to the ratio of the number of times a keyword appears in the web content to the total number of words, usually expressed as a percentage.Although the algorithms of search engines are more complex now, it is no longer the era when simply stacking keywords could achieve good rankings, but moderately paying attention to keyword density can help us ensure that the content theme is clear and convey the core information of the page to the search engine.

2025-11-07

The `index` filter calculates the position of Chinese characters in a string, how many positions does a Chinese character occupy?

In AnQi CMS template development, the `index` filter is a very practical tool that helps us locate the position of a specific substring in a string.However, when dealing with content containing Chinese characters, its performance in position calculation may confuse some users.How many positions does a Chinese character occupy in the `index` filter?What kind of logic is hidden behind this? Let's delve deeper together.### `index` filter's working principle In simple terms

2025-11-07

How to dynamically get the actual character length of the article abstract in AnQiCMS template?

In website content operation, we often need to fine-tune the display form of content, especially for information such as article summaries, which not only affects the first impression of users but also plays an important role in the inclusion of search engines.In AnQiCMS (AnQiCMS) flexible and powerful template system, getting the actual character length of the article abstract is a very practical skill, which can help us achieve more accurate content presentation.Why do you need to get the actual character length of the article summary?The article abstract is the core essence of the content, usually used on list pages

2025-11-07

The `length` filter considers empty values or nil elements when calculating the length of an array or a key-value pair?

AnQiCMS template `length` filter: Deep understanding of its length calculation mechanism During the development of AnQiCMS templates, we often need to judge the length of strings, lists (arrays), or key-value pairs (Maps) to control the display logic of content.The `length` filter is designed for this purpose, it helps us get the length information of these data types.However, many users are confused about whether it considers null or `nil` elements when calculating length. Next

2025-11-07

How to use the `length_is` filter to determine if the character length of a user comment meets the specified requirements?

In the daily operation of AnQi CMS, we often need to manage user-generated content, especially user comments, which directly affect the activity and professionalism of the website.Content length control is one of the common requirements, for example, we hope that the comments are not too short to seem perfunctory, nor too long to affect the reading experience.Anqi CMS's flexible template engine provides a variety of powerful filters that can help us easily implement these seemingly complex validation logic.

2025-11-07