In the template development of AnQi CMS, flexibly handling data structures is the key to dynamic content display. When we need to determine whether a complex data type, such as a key-value pair (map) or a structure (struct), contains a specific key name, the built-in system functioncontainThe filter provides a convenient and efficient solution.

containThe filter is a powerful and intuitive tool provided by the AnQi CMS template engine, allowing developers to check if a data object contains a specific element or key name. Its main feature is to return a boolean value (TrueorFalse),This makes it able to seamlessly combine with conditional judgments in templates (such as{% if %}tags) to achieve intelligent rendering based on data structures.

containThe working principle and syntax of filters

containThe basic syntax of the filter is very concise:

{{obj|contain:关键词}}

Here,objRepresents the data object you want to check, which can be a string, an array (slice), a key-value pair (map), or a structure (struct).关键词This is the specific content you want to search for, which can be a substring of a string, an element of an array, or a key in a key-value pair/structure.

WhenobjIs the key-value pair (map) or structcontainThe filter will check if the object has关键词a complete match withkey name. If it exists, it will returnTrueotherwise, returnFalse.

Actual application example: Check the key name in key-value pairs or structures

In practical template development, the backend may pass various data structures to the frontend, and we often need to adjust the layout or display content based on the integrity of these data structures or the existence of specific fields. Here is how to usecontainFilter check example of whether a specified key name exists in the key-value pair or structure:

Suppose we have a name calledwebInfoThe key-value pair, it may contain the website's title, keywords, and description information:

{% set webInfo = {Title:"安企CMS", Keyword:"AnQiCMS", Description:"免费建站系统"} %}

Now, we want to judge thiswebInfowhether the object containsTitlethis key name:

{% if webInfo|contain:"Title" %}
    <p>网站信息中包含Title字段。</p>
{% else %}
    <p>网站信息中不包含Title字段。</p>
{% endif %}

IfwebInfoit indeed hasTitleIf the key is specified, the page will display "Title field is included in the website information."EnglishThis method is very useful for robustness checking of the data returned by the backend interface, and can avoid errors in template rendering due to the lack of a field.

containThe filter applies not only to key-value pairs but also to structures. IfwebInfois an instance of a structure, it hasTitle/KeywordThe field, the above code can also work in the same way, check if the structure hasTitlethis public field.

More application scenarios

In addition to checking key-value pairs and structure field names,containThe filter also has broader application capabilities:

  • Check if a string contains a specific substring:

    {% set welcomeMsg = "欢迎使用安企CMS(AnQiCMS)" %}
    {% if welcomeMsg|contain:"CMS" %}
        <p>欢迎语中包含了"CMS"。</p>
    {% endif %}
    
  • Check if an array (slice) contains a specific element:

    Suppose we have a list of tags:

    {% set tags = ["Go语言", "CMS", "模板", "开发"] %}
    {% if tags|contain:"CMS" %}
        <p>当前标签列表包含"CMS"标签。</p>
    {% endif %}
    

By these examples, we can seecontainThe powerful features of the filter.It greatly simplifies the conditional logic in templates, allowing us to flexibly control the way pages are presented based on the internal structure or content of data objects, thereby enhancing the dynamism and maintainability of templates.containFilters are indispensable practical tools in the development of secure CMS templates.


Common Questions (FAQ)

  1. Q:containCan filters check key-value pairs (map) or structures (struct)?ValueDoes it contain specific content?A:containThe filter is mainly used to check for the existence of specifickey nameIt does not check these data structures directlyValueDoes it contain a specific content. If you need to check the value of a key, you usually need to get the value of the key first, and then perform the operation on this value.containOr other related filter operations.

  2. Q:containIs the filter case-sensitive when checking?A: Yes,containThe filter is case-sensitive when matching key names or string content. For example,webInfo|contain:"title"andwebInfo|contain:"Title"The result will vary unless your key name is exactly matched. In actual use, please make sure your关键词is exactly consistent with the target key name or string content in terms of case.

  3. Q: BesidescontainFilter, what other similar search or judgment filters are there in the Anqie CMS template?A: The Anqie CMS template engine provides a variety of practical filters for data processing.containSimilarly, filters used for searching or judging include:

    • indexFilter: Used to find the first occurrence of a keyword in a string or array line and return the index value (return -1 if not found).
    • countFilter: Used to calculate the number of times a keyword appears in a string or array line. These filters can be used withcontainFilter combined use, to achieve more refined data judgment and processing logic.