Does the `contain` filter support checking array or Map type data in the AnQiCMS template?

Calendar 👁️ 73

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.

Related articles

How can I determine if a string contains a specific keyword in the AnQiCMS template?

In the AnQiCMS template, dynamically determining whether a string contains a specific keyword is a very practical feature, which can help us implement various intelligent content display and interaction logic on the website front-end.For example, you can display different icons based on whether the article title contains a specific word, or highlight certain key information in the product description.AnQiCMS uses a template engine syntax similar to Django, providing rich tags and filters to process data.

2025-11-08

What string alignment methods do the `ljust` and `rjust` filters implement in the AnQiCMS template?

During the template creation process of Anqi CMS, we often encounter scenarios where we need to format and align text content.Whether it is a list display of product names and prices, or the layout of article titles and dates, a uniform layout can significantly enhance the professionalism and user experience of the website.The Anqi CMS template engine supports syntax similar to Django, providing a series of powerful filters to help us meet these needs.Today, let's discuss in detail two very practical string processing filters: `ljust` and `rjust`

2025-11-08

What filter can be used to center align the string content in the AnQiCMS template?

During the process of AnQiCMS template creation, we often need to fine-tune the text content on the page to achieve better visual effects and information presentation.Among them, the alignment of text is a basic but very important requirement.For the string content in the template, AnQiCMS provides some practical filters (filters) to help us achieve centering, left alignment, or right alignment. To align the string content in the center, we can use the `center` filter built into the AnQiCMS template engine.

2025-11-08

What are the differences between the `capfirst`, `lower`, `upper`, and `title` AnQiCMS filters?

In Anqi CMS template development, in order to better control the display format of content, the system provides a variety of text processing filters.Among them, `capfirst`, `lower`, `upper`, and `title` are some commonly used filters for adjusting the case of English strings.They each have unique purposes and scope, understanding the differences can help us beautify and standardize page text more accurately.Let's discuss their functions one by one and find out the similarities and differences between them.

2025-11-08

How to count the frequency of a keyword in a string in AnQiCMS template?

In AnQi CMS template development, we often need to handle and analyze various data of page content.Among them, counting the number of times a specific keyword appears in a string is a very practical need.This not only helps content operators understand the density of keywords, thereby optimizing SEO, but also provides data support for certain feature displays, such as showing how many times a certain feature is mentioned.The Anqi CMS template engine provides a rich set of filter (Filters) functions, which act as mini data processing tools, allowing variables to be formatted, converted, or calculated

2025-11-08

In AnQiCMS template, can the `count` filter count the number of occurrences of elements in an array?

In AnQiCMS template development, we often need to handle various data, one common requirement is to count the number of times a specific element or keyword appears.When faced with array or string data, the `count` filter can become your powerful assistant.So, can the `count` filter in the AnQiCMS template count the number of occurrences of elements in an array?The answer is affirmative, it can not only count the word frequency in a string, but also accurately count the number of occurrences of elements in an array.

2025-11-08

How to remove specific characters from a string in the AnQiCMS template, such as spaces or special symbols?

In website content operation, we often encounter scenarios where we need to process string data.For example, the title, description, or keywords extracted from the database may contain extra spaces, unnecessary special characters, or even inconsistent prefixes or suffixes.These characters may not only affect the aesthetic beauty of the layout, but may also cause interference when performing data analysis or search engine optimization (SEO).AnQiCMS as an efficient content management system, deeply understands the needs of users in content processing.

2025-11-08

How to format a `time.Time` object into a specified date string in AnQiCMS template?

In website content operation, the way dates and times are presented often directly affects user experience and the clarity of information.A professional, readable date format can not only enhance the credibility of the content, but also allow visitors to obtain key information faster.AnQiCMS as a powerful content management system naturally also provides a flexible way to handle and format date and time.When we need to display the publication time, update time, or any other date information in the AnQiCMS template, we may encounter a problem

2025-11-08