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)
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.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.Q: Besides
containFilter, 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.