AnQiCMS with its efficient, customizable features, brings many conveniences to content management.In the daily operation of websites, we often need to dynamically adjust the page content in templates based on the existence of data.Among these, determining whether a specific key exists in the key-value pair (Map) type of data is a common requirement.containCan the filter be used and how to check for the existence of keys in a Map.

UnderstandingcontainFilter

In the template syntax of AnQiCMS,containA filter is a very practical tool that is used to determine if an object contains another specified value. Its flexibility lies in its ability to be applied to various data types:

  1. String (string):Check if a string contains a specified substring.
  2. Array/slice (slice):Determine if an array contains a specific element.
  3. Key-value pair (Map) or structure (struct):Check if the Map contains the specified key (key), or if the structure contains the specified field name.

No matter which type it acts on,containThe filter will always return a boolean value: if it contains, it will returnTrue; if it does not contain, it will returnFalse. This feature makes it an excellent partner for conditional judgment logic (such as{% if %}tags.

In practice: Check for the existence of a key in a Map (key-value pair)

AnQiCMS template engine indeed supports usingcontainA filter to check if a Map (key-value pair) contains a specific key.This provides great convenience for handling dynamic data passed from the background and rendering template content based on the integrity of the data structure.

Assuming we get a named one from the backendproductDetailsA Map object that may contain product names, prices, and other information, but some products may not includediscount(Discount) this key. We hope to be able to, if it exists in the template,discountIf the key is present, display the discount information, otherwise do not display.

The following is a specific template code example:

{# 假设 productDetails 是一个由后台传递过来的 Map 对象,例如:
   {% set productDetails = {"name":"AnQiCMS高级定制版", "price":199.00, "description":"适用于大型企业,功能丰富"} %}
   或者
   {% set productDetails = {"name":"AnQiCMS基础版", "price":99.00, "discount":"八折优惠", "description":"适用于中小型网站"} %}
#}

<div class="product-info">
    <h3>{{ productDetails.name }}</h3>
    <p>价格:{{ productDetails.price }} 元</p>

    {% if productDetails|contain:"discount" %}
        <p class="discount-label">当前优惠:{{ productDetails.discount }}</p>
    {% else %}
        <p class="no-discount-label">暂无专属折扣信息。</p>
    {% endif %}

    <p>描述:{{ productDetails.description }}</p>
</div>

In this example:

  • productDetails|contain:"discount"The statement will checkproductDetailsIf this Map exists named"discount"key?
  • IfdiscountThe key exists, even if its value isnilor an empty string,containwill be returnedTruethus executing,{% if %}the content within the block.
  • Ifdiscountif the key does not exist at all,containwill returnFalsethen executing instead,{% else %}the content within the block.

In this way, we can easily implement the dynamic rendering of templates, ensuring that the page only displays meaningful and existing data items, and avoids incomplete display or errors due to missing data.

Beyond Map:containOther Uses of Filters

Besides checking the existence of Map keys,containFilters also have many practical applications in AnQiCMS templates:

  • Check for substring in string:For example, we may need to determine if an article title contains a certain sensitive word or specific keyword.
    
    {% if archive.Title|contain:"重要通知" %}
        <span class="highlight">【必看】</span>
    {% endif %}
    <h3>{{ archive.Title }}</h3>
    
  • Check for array elements:Assuming an article has multiple tags (Tag), stored in an array, we can check if it contains a specific tag.
    
    {% set articleTags = archive.Tags|split:", " %} {# 假设Tags是逗号分隔的字符串,先拆分成数组 #}
    {% if articleTags|contain:"SEO" %}
        <span class="tag-seo">SEO优化文章</span>
    {% endif %}
    

These application scenarios all demonstrate.containThe powerful ability of the filter in building dynamic, intelligent website templates.

Cautionary notes and **practice

While usingcontainThere are several points to note when using the filter:

  • Case Sensitive: containThe filter is case-sensitive when checking strings (including Map keys). For example,"discount"and"Discount"are considered two different keys.
  • to enhance readability:When the logic is complex, you cancontainassign the result of the filter to a temporary variable (using{% set %}tags), then perform the judgment, which can improve the readability of the code.
    
    {% set hasDiscount = productDetails|contain:"discount" %}
    {% if hasDiscount %}
        ...
    {% endif %}
    
  • Handle an empty object:If an empty object (nil) is usedcontaina filter, it usually returns silentlyFalseThis will not cause template rendering errors, making it more robust when handling uncertain data.
  • Performance consideration: Although for extremely large-scale data checks,containFilters are usually very efficient, but in very special extreme scenarios, still pay attention to their potential impact on page loading performance.However, for most AnQiCMS websites, this is usually not a problem.

By flexible applicationcontainThe filter allows us to make AnQiCMS templates more intelligent and dynamic, better adapting to diverse content display needs.


Frequently Asked Questions (FAQ)

Q1:containDoes the filter distinguish between uppercase and lowercase when checking Map keys?A1: Yes,containThe filter is strictly case-sensitive when checking the key names of Map. For example, if you want to checkproductDetailsDoes it exist"Discount"Then the key,productDetails|contain:"discount"It will returnFalsebecause the key name does not match completely.

Q2: If a Map contains a certain key but its value isnilor an empty string,containwhat will the filter return?A2: containThe filter only checks if the key (key) exists, and does not check the value corresponding to the key (value). Therefore, as long as the key exists in the Map, even if its value isnilComma, empty string or any other value,containThe filter will returnTrue.

Q3:containCan the filter directly check the keys in a nested Map? For example, the value of a key in the Map is also a Map.A3: containThe filter can only check the keys at the current level. If you need to check the keys in a nested Map, you must first obtain the internal Map object and then use it againcontainThe filter checks. For example:{% if parentMap.childMap|contain:"nestedKey" %} ... {% endif %}.