Can `contain` filter be used in AnQiCMS template to check if a key exists in a map or struct?

Calendar 👁️ 87

In AnQi CMS template development, 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 system built-incontainThe filter provides a convenient and efficient solution.

containThe filter is a powerful and intuitive tool provided by 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 (TrueorFalseThis makes it possible 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 syntax of the filter is very concise:

{{obj|contain:关键词}}

Here, objrepresent the data object you want to check, it can be a string, an array (slice), a key-value pair (map), or a struct.关键词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 a key-value pair (map) or a structure (struct) when,containThe filter checks whether the object has a match for,关键词an exact match of,key name. If it exists, it will returnTrue; otherwise, it returnsFalse.

Practical application example: check the key name in key-value pairs or structures

In actual 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 completeness of these data structures or the existence of specific fields. The following is how to usecontainThe filter checks for the existence of a specific key name in a key-value pair or a structure:

Suppose we have a namedwebInfoThe key-value pair, which may contain the title, keywords, and description information of a website:

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

Now, we want to judge whether thiswebInfoobject containsTitlethis key name:

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

IfwebInfodoes indeed haveTitleThe key, then the page will display "Website information includes the Title field."This method is very useful for robustness checks of the back-end interface return data, as it can avoid errors caused by missing fields in template rendering.

containThe filter is not only applicable to key-value pairs, but also to structures. IfwebInfois an instance of a structure, possessingTitle/Keywordfields, the code above can also be used to check whether the structure hasTitlethis public field.

more application scenarios

In addition to checking key-value pairs and structure key names,containThe filter also has a wider range of 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:

    Assuming we have a list of tags:

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

Through these examples, we can seecontainThe power of the filter is great. It greatly simplifies the conditional logic in templates, allowing us to flexibly control the presentation of the page based on the internal structure or content of the data object, thereby enhancing the dynamics and maintainability of the template.No matter in complex business logic judgment, or simple content verification,containFilters are essential practical tools in the development of secure CMS templates.


Frequently Asked Questions (FAQ)

  1. Q:containCan the filter check the key-value pair (map) or structure (struct)?ValueDoes it contain specific content?A:containThe filter is mainly used to check for the existence of specifickey name. It does not directly check the internal structure of these data structuresValueDoes 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 performcontainor other related filter operations.

  2. Q:containDoes the filter distinguish between uppercase and lowercase 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 results will vary unless your key name matches exactly. In actual use, please make sure that your关键词matches the target key name or string content exactly in terms of case.

  3. Q: BesidescontainFilter, what similar search or judgment filters are available for AnQi CMS template?A: The AnQi CMS template engine provides a variety of practical filters for data processing. WithcontainFilters that are similar, including those used for search or judgment:

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

Related articles

How to determine if a specific value exists in an array (slice) while developing an AnQi CMS template?

During the development of Anqi CMS templates, we often encounter scenarios where we need to determine whether an array (slice) contains a specific value.For example, you may need to dynamically adjust the display of content based on whether the user tag exists in a predefined tag list;Or when handling complex business logic, determine whether a permission ID exists in the current user's permission set.For this requirement, AnQiCMS template engine provides a concise and powerful solution, allowing developers to handle these logic in an elegant way.In the AnQiCMS template system

2025-11-07

How to use the `contain` filter to check if the user's input text contains the preset brand name?

In daily website content operations, we often need to standardize the management of user input or content generated by the system, especially when it involves brand names.Maintaining the consistency and accuracy of the brand name is crucial, not only for enhancing the brand image, but also for the SEO performance of the website, legal compliance, and user experience.AnQi CMS provides a flexible and powerful template engine, where the `contain` filter is a very practical tool that can help us efficiently check if the text contains the preset brand name.Why check the brand name in the content

2025-11-07

How to quickly judge whether a string of text in the AnQiCMS template contains specific sensitive words?

In website content operation, compliance and security are of great importance.Especially for platforms with user-generated content (UGC) or when publishing articles, product information, we often need to quickly determine if a string of text contains specific sensitive words.AnQiCMS is an efficient enterprise-level content management system that also provides very flexible and convenient tools at the template level, allowing you to easily meet this requirement.

2025-11-07

How to display the links and titles of the previous and next related articles on the document detail page of Anqi CMS?

In Anqi CMS, adding links and titles of "Previous Article" and "Next Article" to the document detail page is a very practical feature to enhance user experience and content consistency.This not only encourages users to browse more content, but also helps optimize the internal links of the website.AnQi CMS provides simple and efficient template tags, allowing us to easily meet this requirement.

2025-11-07

What is the difference in the judgment logic of the `contain` filter when processing Chinese string and English string?

In AnQi CMS template design, we often use various filters to process and judge data.Among them, the `contain` filter is a very practical tool that can help us quickly determine whether a text, array, or object contains specific keywords.Many users may be curious about whether there is a difference in the judgment logic of the `contain` filter when processing Chinese and English strings.

2025-11-07

How to store the judgment result of the `contain` filter in a variable for subsequent complex logic judgment?

In AnQi CMS template development, we often need to dynamically display or hide certain elements based on specific content conditions, or execute different logical branches.It is straightforward to output the result of a judgment directly in a template, but when you need to perform more complex logical branches based on this judgment, direct output seems inadequate.At this point, storing the judgment result in a variable has become the key to achieving fine-grained control.

2025-11-07

When do you need to judge whether multiple keywords exist in a string, does the `contain` filter have a batch processing mechanism?

In the daily operation of website content, we often encounter such a scenario: we need to judge whether a document, a page title, or any text content contains multiple keywords that we have preset.For example, we might want to know if an article mentions both 'AnQi CMS' and 'Content Operation', or at least mentions 'Go language' or 'High performance'.At this time, many friends will naturally think of the powerful `contain` filter in the AnQiCMS template engine.Then, when you need to judge whether multiple keywords exist in a string

2025-11-07

In the AnQiCMS template, can the `contain` filter flexibly configure case-sensitive keywords?

AnQiCMS provides a rich set of filters in templates for data processing and display, where the `contain` filter is a practical tool frequently used to determine if the content includes specific keywords.When using such filters, we often encounter issues related to case sensitivity, which directly affects the accuracy of search and filtering results.

2025-11-07