During the process of website content display or template development, we often encounter situations where we need to process specific text, such as checking if a keyword exists or locating the first occurrence of a character or substring.The template engine of AnQiCMS provides a series of powerful filters (Filters) to help us complete these tasks efficiently.indexFilter, accurately find the index of the first occurrence of a character or substring in AnQiCMS templates.
UnderstandingindexFilter
AnQiCMS template engine provides a namedindexThe built-in filter, which is specifically used to address the need for locating the position of characters or substrings.This filter helps us quickly find the starting index of the first occurrence of a character or substring in a given text (string or array).
Its basic usage is very intuitive:{{ obj|index:"关键词" }}
Among them:
objRepresents the string or array you want to search."关键词"Is the character or substring you want to find.
IfindexThe filter successfully found the keyword, it will return the starting index of the first occurrence of the keyword, and this index is from0Counting starts. If the keyword does not exist inobj, it will return-1.
In practical use, special attention should be paid to the handling of Chinese characters.AnQiCMS template engine calculates string indices based on byte length rather than character length.indexFilter returns the index value. Therefore, when processing text containing Chinese characters, please make logical judgments based on the understanding of byte length.
Actual application scenarios and examples.
Scene one: Find the first occurrence position of a substring in ordinary text
We assume that we have a text introducing an Ant Security CMS, and we want to know where the substring 'CMS' first appears.
{% set introduction = "欢迎使用安企CMS(AnQiCMS)" %}
{{ introduction|index:"CMS" }}
The output of this code will be18。
Let's simply analyze this index value:
欢/迎/使/用/安/企:Each Chinese character occupies 3 bytes. Therefore, the first six Chinese characters account for6 * 3 = 18bytes.C:The English characters following itCThe index of the is18. Therefore,indexFilter returns18is logically consistent with byte counting.
Scenario two:配合{% set %}Perform conditional judgment
In template development, we usually do not directly display the index value to the user, but use this index to drive some logical judgments, such as displaying or hiding an element based on whether a substring exists.
{% set page_content = "AnQiCMS 是一个高效的内容管理系统,深受用户喜爱。" %}
{% set keyword_position = page_content|index:"高效" %}
{% if keyword_position != -1 %}
<p>内容中提到了“高效”这个词!</p>
{% else %}
<p>内容中未发现“高效”这个词。</p>
{% endif %}
This code will checkpage_contentwhether it contains the word "efficient".keyword_positionwill not be equal to-1Therefore, the page will display 'The content mentions the word 'efficient'!' This way, the template can dynamically adjust the display according to the content.
Scene three: Find the first occurrence position of an element in an array.
indexThe filter can be used not only for strings but also for finding the first occurrence position of a specific element in an array.
{% set categories = ["网站新闻", "产品介绍", "技术文章", "产品介绍"] %}
{{ categories|index:"产品介绍" }}
The output of this code will be1In the array,indexThe filter will return the index position of the target element in the array, also from0Start counting. Even if there are multiple identical elements in the array, it will only return the first one that appears.
What are the points to note
- Return the first occurrence:
indexThe filter always returns the position of the first occurrence of the keyword. If you need to get all occurrences, this is beyondindexThe range of filter capabilities may require more complex template logic or processing before content is stored. - Case sensitive:String matching is usually case-sensitive. For example, searching for
cmsandCMSMay result in different outcomes. - Returns
-1The meaning of:Use wisely-1This return value to judge whether the keyword does not exist, which is very useful when building conditional logic.
PassindexFilter, we can easily locate the exact text in AnQiCMS templates, thereby realizing a more flexible and intelligent content display strategy.
Common Questions (FAQ)
Q1:indexFilter whether the regular expression search is supported?
A1: indexThe filter does not support regular expressions.It performs an exact match search for ordinary strings or array elements.If you need to perform more complex matching based on regular expressions, you may need to consider preprocessing the content at the content management or program level, and then passing the processed data to the template.
Q2: How to determine if a string contains another substring without knowing the specific position?
A2:If you just want to determine if it exists, without needing to know the specific location, the AnQiCMS template engine providescontainfilter. For example:{% if "欢迎使用安企CMS"|contain:"CMS" %} 包含 {% else %} 不包含 {% endif %}This will return a boolean value (True or False), which is more concise and clear.
Q3:indexThe index returned by the filter is based on characters or bytes? If there are both Chinese and English characters in the string, how can the index be accurately determined?
A3: indexThe index returned by the filter is based onbytesEnglish.As mentioned in the article, Chinese characters typically occupy multiple bytes (such as 3 bytes) under UTF-8 encoding, while English characters occupy 1 byte.Therefore, in mixed Chinese and English strings, the index value needs to be calculated and understood according to the byte length of character encoding.indexThe result has advanced by 3 units, so the index of the next character (whether Chinese or English) will start counting from 3 units after this.