In the daily operation and template design of AnQiCMS, we often need to determine how to display it based on the specific attributes of the content.For example, you may want to highlight articles with a specific keyword in the title, or adjust the style based on whether there is a specific word in the product description.How to determine whether a string contains another substring in a template has become a very practical skill.

The AnQiCMS template system is based on the Django template engine syntax, providing rich tags and filters to help us flexibly handle data.We can cleverly utilize several built-in filters for string inclusion judgments.

Core method one: utilizingcontainDirect judgment by filter

The most direct and recommended method is to usecontainA filter. This filter is specifically used to determine whether a string, array, or map (map/struct) contains a certain substring or key.Its return value is a boolean value (TrueorFalse),It is very suitable for use in conditional judgments.

Its basic usage is very simple, you just need to take the string to be checked asobj, and the substring to be searched as parameters:

{{ obj|contain:"子串" }}

For example, let's assume we have an article titlearchive.TitleTo determine if it contains the word 'CMS':

{% if archive.Title|contain:"CMS" %}
    <span style="color: red;">文章标题中包含“CMS”!</span>
{% else %}
    <span>文章标题中不包含“CMS”。</span>
{% endif %}

In the above example, ifarchive.TitleThe value is 'Welcome to AnQiCMS (AnQiCMS)', then the page will display 'The article title contains 'CMS'!'.

containThe strength of the filter also lies in its versatility. It can not only check ordinary strings, but also search in arrays (slice) and key-value pairs (map/struct).

For example, if you have an array of article tagstagsto check if it contains a specific tag:

{% set tags = "Go语言,内容管理,SEO优化"|split:"," %} {# 假设tags是一个字符串,我们先用split过滤器转换为数组 #}
{% if tags|contain:"SEO优化" %}
    <p>这篇文章与SEO优化相关。</p>
{% endif %}

Or, if you have a mapping that contains additional configuration informationconfigWould you like to check if a key exists:

{% set extraConfig = { author:"AnQiCMS团队", release_date:"2023-01-01" } %}
{% if extraConfig|contain:"author" %}
    <p>作者信息已配置:{{ extraConfig.author }}</p>
{% endif %}

Core method two: utilizingindexFilter assists in judgment

exceptcontain,indexFilter is also a very useful tool.indexThe filter returns the position of the first occurrence of the substring in the target string (starting from 0). If the substring does not exist, it returns-1We can use this feature to indirectly determine if a substring exists.

Its basic usage is as follows:

{{ obj|index:"子串" }}

For example, we still use the article title.archive.Title:

{% if archive.Title|index:"CMS" != -1 %}
    <span style="font-weight: bold;">发现“CMS”字样,位置在 {{ archive.Title|index:"CMS" }}!</span>
{% else %}
    <span>未发现“CMS”字样。</span>
{% endif %}

AlthoughindexCan also implement the same judgment logic, but usually, if it is just to judge whether it exists,containThe filter will be more concise and intuitive, and also more in line with semantics. If you need to get the specific position of a substring, thenindexThe filter is your best choice.

Practical skills and precautions

  1. Make good use of{% set %}Tags:When you make complex judgments or need to refer to judgment results multiple times, you can store the filter results in a variable, which can improve the readability and maintainability of the template:

    {% set hasSpecialKeyword = archive.Title|contain:"重要" %}
    {% if hasSpecialKeyword %}
        <span class="highlight-label">重要文章</span>
    {% endif %}
    
  2. Case sensitivity:By default,containandindexFilters are allCase sensitiveThis means that "CMS" and "cms" are considered different substrings.If you need to make a case-insensitive judgment, you can consider converting the string to a uniform case before comparison, for example:

    {% if archive.Title|lower|contain:"cms" %} {# 将标题全部转为小写后再判断 #}
        <span class="info">标题中不区分大小写包含“cms”</span>
    {% endif %}
    
  3. Support for Chinese:The filter of AnQiCMS supports Chinese well, no mattercontainOrindex, it can correctly handle Chinese string.

With these simple and powerful filters, you can flexibly judge whether a string contains a specific substring in the AnQiCMS template, thus realizing more dynamic and intelligent content display and page logic.


Frequently Asked Questions (FAQ)

1.containIs the filter case sensitive?

Yes,containThe filter is case sensitive by default. For example,"AnQiCMS"|contain:"cms"It will returnFalseIf you need to perform a case-insensitive judgment, it is recommended to first convert the original string or substring through|loweror|uppera filter to uniform case and then compare.

2. Can I determine if a string contains any of multiple substrings?

Of course. You can use multiplecontainthe filter meetsiflabel'sorlogical operators together. For example:

{% if article.Title|contain:"Go语言" or article.Title|contain:"AnQiCMS" %}
    <p>这篇文章与Go语言或AnQiCMS相关。</p>
{% endif %}

If you need to determine whether it contains all the specified substrings, you can useandoperator.

3. Besides strings,containWhat data types can the filter check?

containThe filter is very flexible, it can check not only normal strings, but also:

  • array (slice): Determine if a certain value exists in the array.
  • (map) or structure: Check if a certain key name exists. For example,["Apple", "Banana"]|contain:"Apple"will returnTruewhile{ "name":"Alice" }|contain:"name"will be returnedTrue.