During the template development process of AnQi CMS, we often need to decide how to display the page based on the specific content of the data, or determine whether a particular element exists in the data we pass in.When dealing with complex arrays or key-value pairs (Map), it is particularly important to make efficient and accurate judgments of this kind.containThe filter has become a very practical tool.

containFilter: flexibly judge whether the data contains specified content.

containThe filter plays a core role in the Anqi CMS template engine, allowing us to check if a data collection (such as a string, array, Map, or structure) contains a specific value or key. The strength of this filter lies in its adaptability, as it can handle different types of data and return a clear boolean value (TrueorFalse),greatly simplifies the logic of conditional judgments.

When you need to judge the following situations in the template,containfilters can provide direct answers:

  1. whether a substring is contained in a string:For example, check if an article's title contains a specific keyword.
  2. Whether an element is contained in an array (or a slice in Go language):For example, a document may be associated with multiple tags, and you need to determine if a specific tag exists.
  3. Whether a key name is included in a key-value pair (Map) or a structure (struct):For example, if you have a configuration Map, you need to determine whether a configuration item exists.

No matter what form your data is in,containthe way filters are used is consistent, and the basic syntax is:{{ obj|contain:"关键词" }}.objrepresents the data variable you want to check, and"关键词"is the content you want to find.

Example of practical application scenarios

Let us understand through several specific examplescontainHow does the filter work in the Anqi CMS template.

Example one: Check if a string contains specific text.

Assume you want to display a special prompt on the page if some descriptive text mentions "discount".

{% set description = "本店所有商品限时优惠,欢迎选购!" %}
{% if description|contain:"优惠" %}
    <p class="promo-text">🎉 机会难得,立即抢购!</p>
{% else %}
    <p>欢迎浏览本店商品。</p>
{% endif %}

This code will first assign a string todescriptionthe variable, and then usecontainThe filter checks if it contains the words 'discount'. Since the example contains them, the page will display '🎉 Great Opportunity, Grab It Now!'.

Example two: Determine if an element exists in an array

Assuming you have a list of products, each product hastagsan array (for example: ["新品", "热销", "推荐"])。Now, you want to filter all products with the 'hot sale' tag.

{% set product_tags = ["新品", "热销", "限量"] %}
{% if product_tags|contain:"热销" %}
    <span class="badge hot-sale">热销商品</span>
{% else %}
    <span class="badge normal">普通商品</span>
{% endif %}

Here, product_tagsis regarded as an array,containThe filter checks if there is a string element called "hot-selling

Example three: Check if a Map or struct contains a specified key name

When dealing with dynamic data, sometimes it is uncertain whether a certain key exists in the传入 the Map or structure. For example, you have a page configurationpage_config, you need to determine if it is definedbannerkey.}

{% set page_config = {"title": "关于我们", "content": "公司简介", "sidebar": true} %}
{% if page_config|contain:"banner" %}
    <div class="page-banner">
        <!-- 显示页面横幅内容 -->
    </div>
{% else %}
    <div class="no-banner-placeholder">
        <!-- 页面无横幅 -->
    </div>
{% endif %}

In this example,containThe filter is used to checkpage_configIf there exists a named in Mapbannerthe key. Becausepage_configThere is no such key, so the condition is not met, the page will display 'No banner on the page'. It is worth noting that for Map or structure,containthe filter checks itskey nameand not the key-value pair.

Summary

containThe filter is a simple yet powerful tool in the Anq CMS template engine, making conditional judgments in templates more intuitive and efficient.Whether it is for fuzzy matching of strings or for precise searching of array elements or Map keys, it can provide a reliable boolean result to help you build more dynamic and intelligent website content.Mastering this filter will undoubtedly give your CMS template development work a flying start.


Frequently Asked Questions (FAQ)

  1. How to determine if an array or string does not contain a specific keyword?You can usenotKeyword andcontainFilter combined to judge the negative situation. For example:{% if not my_string|contain:"关键词" %}Or{% if not my_array|contain:"元素" %}.

  2. containDoes the filter distinguish between uppercase and lowercase when checking strings?Yes, by defaultcontainThe filter is case-sensitive. For example,"AnQiCMS"|contain:"cms"will returnFalseBecause it cannot find a substring that matches lowercase "cms" completely.If you need to perform a case-insensitive check, you may need to preprocess the string (such as converting it to uppercase or lowercase) before passing it to the template, or consider creating a more complex filter.

  3. Can I usecontainThe filter to check the values in nested arrays or Maps? containThe filter directly checks the data at the current level (substring of a string, an element of an array, a key of a Map). If you need to check the values in a nested structure, you usually need to go throughforThe loop or other template logic first traverses to the corresponding nested level, and then uses it at that levelcontainCheck the filter. For example, to check if a name exists in an array of multiple user Maps, you need to traverse the array and check in each user Map.nameThe value of the key.