In the template world of AnQiCMS, efficiently processing and displaying content is the core.Whether it is to dynamically adjust the page display based on user input or to quickly locate key information in complex text, mastering the template functions can make your website content operation twice as effective.indexFilter.

indexFilter: string 'Locate Expert'

Imagine that you have a long piece of text or a list with multiple tags, and you need to quickly know if a specific word or tag exists and where it first appears.indexThe filter comes into play.

indexThe main function of the filter isFind the first occurrence position of the specified "keyword" in a string or an array (slice)It is like a precise navigation instrument, able to tell you where the target string or element starts.

When you useindexFiltering, it will return an integer. This integer represents the keyword in the original string.Starting from 0The starting position.If unfortunately, it does not find the keyword you are looking for, it will return a special number: -1.This -1 is like telling you: 'Sorry, I didn't find it.'

It is worth noting that when dealing with strings containing Chinese characters,indexFilter has a unique rule when calculating position:A Chinese character is counted as 3 position lengths.This means that if you are looking for a substring in a Chinese string and want to use the returned position for subsequent operations, you need to consider this feature.例如,在一个包含“欢迎使用安企CMS”的字符串中查找“CMS”,如果“欢迎使用安企”恰好是6个中文字符(6 * 3 = 18),那么“CMS”的起始位置将是18。

Actual application scenarios: Make content more intelligent

indexThe filter is simple, but its application can bring smarter interaction and more flexible content display to your website.

  1. Conditional display or style adjustment: Assume you want to add a prominent tag or change the color of the title when the article title contains a specific keyword. You can useindexFilter to determine if a keyword exists:

    {% set articleTitle = "安企CMS:打造高效企业级网站" %}
    {% if articleTitle|index:"高效" != -1 %}
        <h1 style="color: blue;">{{ articleTitle }} <span class="highlight-tag">热门</span></h1>
    {% else %}
        <h1>{{ articleTitle }}</h1>
    {% endif %}
    

    This code will check if the title contains the word 'efficient'. If found, it will add a blue style and a 'hot' tag to the title.

  2. Dynamic content extraction and processing AlthoughindexIt does not modify the content itself, but its result can guide other filters to perform operations.For example, you want to find a specific phrase in a long text and cut it off before the phrase, or add specific information after the phrase.sliceFilter to achieve more accurate text processing.

  3. Existence check of array elements: In addition to strings,indexThe filter can also be used for arrays. If you have a product tag array and want to quickly determine if a tag has been added:

    {% set productTags = ["新品上市", "限时优惠", "推荐"]|list %}
    {% if productTags|index:"限时优惠" != -1 %}
        <p>此产品正在限时优惠中!</p>
    {% else %}
        <p>此产品暂无特殊优惠。</p>
    {% endif %}
    

    Here,productTags|listensuringproductTagsit is parsed correctly as an array, thenindexFilter determines if "time-limited promotion" exists.

Summary

indexThe filter is a powerful and flexible tool in AnQiCMS template engine, which can help you accurately locate keywords in strings or arrays, providing the foundation for dynamic and intelligent content display.Master its usage, especially the calculation rules for Chinese characters, will make your template development more intuitive, thereby enhancing the overall operational efficiency and user experience of the website.

Common Questions and Answers (FAQ)

Q1:indexDoes the filter distinguish between uppercase and lowercase when searching for strings?A: Yes,indexThe filter is case-sensitive when performing string searches. For example, searching for 'CMS' and searching for 'cms' will be considered two different keywords.

Q2: BesidesindexEnglish: Are there other methods in AnQiCMS to check if a keyword is contained in a string? What are the differences between them?English: A: AnQiCMS providescontainFilter, it can also be used to check if a keyword is included in a string or an array. The main difference lies in the return value:

  • indexFilter returns the keywordThe position of the first occurrence(An integer, -1 if not found)
  • containThe filter returns one directlyBoolean value(True or False),indicates whether the keyword exists. If you only need to know 'whether or not',containit will be more concise; if you also need to know 'where', thenindexthis is a better choice.

Q3: If I need to find all the positions of the matches in a string,indexCan the filter do that?A:indexThe filter can only return the keywordsThe position of the first occurrence. If you need to find all the positions of the matches,indexThe filter cannot be implemented directly.This scenario usually requires combining with more complex template logic (such as, using loops and string slicing to gradually search), or passing the result to the template after processing on the backend (AnQiCMS's Go language logic layer).