How to use the `filter-contain` filter in AnQiCMS to check string or array containment in the `if` statement?

Calendar 👁️ 61

As an experienced website operations expert, I am well aware that efficient content management and flexible page display are crucial for enhancing user experience and SEO effectiveness.AnQiCMS (AnQiCMS) offers a series of practical tools with its high-performance architecture based on the Go language and the powerful functionality of the Django template engine.filter-containLook how it handles in AnQiCMS'sifJudgment, helping us flexibly check the inclusion relationship of strings or arrays, thus unlocking more intelligent and dynamic content presentation methods.

Secure CMS template tool:filter-containUnlock new ways to judge dynamic content with filters,

In the daily work of content operation, we often need to decide whether to display the content on the page or in what form based on certain conditions.For example, when the title of an article contains specific keywords, we may want to add a "hotfilter-containThe filter is the powerful assistant to solve such "inclusion relationship" judgments, it can simply and efficiently check if there is specific content in strings, arrays, even key-value pairs (map) or structures (struct), and return a boolean value (TrueorFalsein the form returned, perfectly fits our template inifjudgment logic.

filter-contain: The core tool for detecting content inclusion relationships

filter-containThe core function of the filter is to determine whether a target object (which can be a string, array, map, or struct) contains the specified keyword or element. The definition of 'contains' varies according to the type of the target object, but it will always return a clearTrueorFalseLet our template logic become clear and powerful. Its basic usage is very intuitive:{{obj|contain:关键词}}.

Now, let's understand it in detail through several specific scenariosfilter-containactual application.

Check if a string contains specific text

This isfilter-containOne of the most common application scenarios.Imagine, you want to add a prominent corner mark or adjust the style for articles in the list that contain 'AnQiCMS'.filter-contain:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5 class="title">{{item.Title}}</h5>
            {% if item.Title|contain:"AnQiCMS" %}
                <span class="badge">安企CMS相关</span>
            {% endif %}
            <p>{{item.Description}}</p>
        </a>
    </li>
    {% endfor %}
{% endarchiveList %}

In this code, we traverse the list of articles byitem.Title|contain:"AnQiCMS"Determine whether the current article title contains the keyword "AnQiCMS". If it contains, render a text with the words "AnQiCMS related".spanThe ability to dynamically display content is very useful for enhancing the interactivity and precision of website information display.

2. Determine if a specific element exists in the array (Slice)

In AnQiCMS, we often tag articles or products with multiple tags (Tags), which are usually stored in array form. Suppose we want to check if an article is tagged with the "SEO optimization" tag to display a specific prompt message,filter-containIt can still be used:

{% archiveDetail archive with name="Content" %}
    <article>
        <h1>{{archive.Title}}</h1>
        <div class="article-tags">
            {% tagList tags with itemId=archive.Id limit="10" %}
            {% set tagTitles = [] %}
            {% for tag in tags %}
                {% set tagTitles = tagTitles|add:tag.Title %} {# 收集所有标签标题到tagTitles数组 #}
                <a href="{{tag.Link}}">{{tag.Title}}</a>
            {% endfor %}
            {% if tagTitles|contain:"SEO优化" %}
                <p class="seo-tip">这篇内容特别针对SEO优化进行了深入探讨!</p>
            {% endif %}
            {% endtagList %}
        </div>
        <div class="article-content">
            {{archive.Content|safe}}
        </div>
    </article>
{% endarchiveDetail %}

Here we first obtained all the tag titles of the article and constructed atagTitlesarray, and then usedtagTitles|contain:"SEO优化"Check if the array contains the item 'SEO optimization'. This way, we can dynamically provide additional information or features based on the characteristics of the article.

3. Check if the key-value pair (Map) or structure (Struct) contains a specific key name

filter-containWhen dealing with key-value pairs (map) or structures (struct), their behavior is slightly different, it checks whether a specific key name exists in these objects, rather than checking the key value.This is particularly useful when dealing with custom content models.

For example, in AnQiCMS, we can define custom fields for different content models.Assuming we want to determine on a document detail page whether the content model of the document contains a custom field named "author".

{% archiveDetail archive with name="Content" %}
    <article>
        <h1>{{archive.Title}}</h1>
        {# 假设 archive.Extra 是一个包含所有自定义字段键值对的结构体或map #}
        {% archiveParams params with sorted=false %}
        {% if params|contain:"author" %}
            <p>作者:{{ params.author.Value }}</p>
        {% endif %}
        {% if params|contain:"source" %}
            <p>来源:{{ params.source.Value }}</p>
        {% endif %}
        {% endarchiveParams %}
        <div class="article-content">
            {{archive.Content|safe}}
        </div>
    </article>
{% endarchiveDetail %}

In this example,archiveParamsThe tag obtained all the custom parameters of the document and used them asparamsVariable passed. Throughparams|contain:"author"We judgeparamsWhether there exists a key named 'author' in this key-value pair (or structure). If it exists, we can safely access itparams.author.ValueTo display the author information, it can avoid template rendering errors due to the absence of fields.

Expansion of operation strategy application

filter-containFlexible application, can bring many conveniences to our content operation:

  • Intelligent SEO optimization: By checking whether the article title and description contain core keywords, SEO-friendly prompts can be dynamically added, or the page structure can be adjusted to assist search engines in better understanding the content.
  • Personalized content recommendation: Based on user behavior or tag preferences, combine the tags or attributes of the content to dynamically filter and display a list of content that is more in line with user interests.
  • Dynamic feature switchIn a multi-site or multi-template scenario, you can decide whether to load a specific JavaScript, CSS, or feature module based on certain characteristics of the current site or template (such as the existence of certain system settings).
  • Content Quality ControlFor example, check if the user's submitted comment contains certain sensitive words, or if the article content conforms to specific requirements (although such checks are usually performed on the backend, but auxiliary hints during frontend display are also useful).

Summary

filter-containThe filter is a powerful and practical tool in the AnQiCMS template engine, it greatly simplifies the complexity of our content conditional judgments with its intuitive syntax and boolean return mechanism.Whether it is to check the inclusion of simple strings or to explore specific elements or key names in arrays and key-value pairs, it can help us build a smarter and more dynamic AnQiCMS website.Master and effectively use this filter, no doubt it will make your website operation more efficient, flexible, and provide users with a more customized content experience.


Frequently Asked Questions (FAQ)

Q1:filter-containAre string or array comparisons case-sensitive?A1: Yes,filter-containString contains comparisons are case-sensitive. For example,"AnQiCMS"|contain:"cms"will returnFalse. If you need to perform a case-insensitive judgment, you may need to convert the target string and keywords to the same case (such as lowercase) before judgment, but AnQiCMS built-inlowerorupperThe filter can assist in achieving this, for example{{ item.Title|lower|contain:"anqicms" }}.

Q2: Can I usefilter-containDo you want to check the relationship between multiple keywords at once? For example, do I want to know if an article contains both 'SEO' and 'marketing'?A2:filter-containYou can only check one keyword at a time. If you need to check a situation where multiple keywords are included, you can combine multiplecontainPass judgmentifThe logical operator of tags (such asandoror) together. For example:

Related articles

How to use the `if` tag in combination with `forloop.Counter` to alternate the odd and even row styles of list items?

As an experienced website operation expert, I fully understand how to enhance user experience through detailed interface design in content presentation.AnQiCMS, with its high-performance architecture based on Go language and flexible template engine, provides us with great freedom to easily implement all kinds of creativity and features.

2025-11-06

How to elegantly handle empty list situations in AnQiCMS template using the `{% for ... empty ... %}` syntax?

In AnQiCMS template development, we often need to display a series of content lists, such as article lists, product lists, navigation menus, or friend links.However, these lists are not always filled with data. When the list is empty, how to elegantly inform the user that "there is no content here" instead of displaying a blank page or an error message has become a detail consideration in template design.AnQi CMS is well-versed in this, drawing on the excellent design of Django templates in its powerful template engine developed in Go language, providing us with `{% for ...

2025-11-06

How to use the `IsCurrent` property to mark the currently selected filter condition in the `archiveFilters` filter tag?

As an experienced website operation expert, I deeply understand the critical role of user experience (UX) in the success of a website.A user-friendly, responsive interface can effectively guide users to discover content, and the content filtering feature is an important aspect of improving user experience.In AnQiCMS (AnQiCMS) this efficient and customizable content management system, the `archiveFilters` tag provides strong support for building flexible filtering functions, and its internal `IsCurrent` property is the 'magic wand' that lights up the user experience. Today

2025-11-06

How to determine if a custom field exists or has a value in `archiveParams` before displaying it?

As an experienced website operation expert, I deeply know the powerful potential of AnQiCMS (AnQi CMS) in content management and website optimization.The flexible content model and customizable fields provide great convenience for us to build highly personalized websites.However, how to elegantly handle these dynamically generated custom fields in template design, ensuring that they are displayed only when they exist or have a value, is the key to improving template quality and user experience.

2025-11-06

How to use the `filter-divisibleby` filter to determine the divisibility of a number in an `if` statement?

In AnQiCMS template development, achieving dynamic content display and fine-grained control is a daily challenge for website operation experts.AnQiCMS is highly praised for its concise and efficient template engine, which inherits the elegant style of Django templates, making content display and logical control intuitive.In website operation, we often need to present different content or styles based on the characteristics of numbers, such as displaying an advertisement every few products or setting different background colors for odd and even rows of the list.At this time, the `divisibleby` filter can be perfectly combined with the `if` statement

2025-11-06

How to use the `filter-length_is` filter in AnQiCMS templates for `if` judgment of the exact length of a collection?

## Precise Control and Intelligent Presentation: Efficient Application of the `filter-length_is` Filter in AnQiCMS Templates In the template development practice of AnQiCMS, we often need to finely control the displayed data to ensure the accurate transmission of content and the elegant presentation of the user interface.AnQi CMS provides powerful tools for content operators and developers with its efficient architecture based on the Go language and the flexible syntax of the Django template engine, among which

2025-11-06

What is the difference in handling variable null values between `filter-default` and `filter-default_if_none` in `if` condition judgments?

AnQiCMS (AnQiCMS) is a powerful assistant for our daily content operation, with its flexible template engine syntax making content display diverse and efficient.However, in practice, we often encounter situations where variable values are empty, such as when the article title is not filled in, or when a custom field has no data.How elegantly to handle these 'null' values to ensure that the website front-end display is always professional and friendly has become a topic that content operators must face.

2025-11-06

How does the `filter-yesno` filter help AnQiCMS templates handle the tri-state logic of 'yes/no/unknown'?

As an experienced website operations expert, I know that how to present data status efficiently and clearly in a content management system is one of the keys to successful operations.AnQiCMS (AnQiCMS) provides an excellent solution in this aspect with its flexible template engine and rich built-in filters.Today, let's delve into a seemingly simple yet powerful filter——`filter-yesno`, and how it helps AnQiCMS templates handle complex ternary logic such as 'yes/no/unknown'.

2025-11-06