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

AnQiCMS's template system is based on Django template engine syntax, which provides rich tags and filters to help us flexibly handle data.For string inclusion checks, we can cleverly utilize several built-in filters.

Core method one: usingcontainDirect judgment with filter

The most direct and recommended method is to usecontainFilter.This filter is specifically used to determine whether a string, array, or map (map/struct) contains a certain substring or key.TrueorFalse),非常适合在条件判断中使用。

Its basic usage is very simple, you just need to take the string to be checked asobj,要查找的子串作为参数即可:

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

For example, 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 find in arrays (slice) and key-value pairs (map/struct).

For example, if you have an array of article tagstagsand you want to 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 informationconfig,Want to judge 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: UtilizeindexFilter auxiliary judgment

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

The basic usage is as follows:

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

For example, we still use the article titlearchive.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 only for judging whether it exists,containThe filter will be more concise and intuitive, and also more semantically appropriate. If you need to get the specific position of a substring, thenindexthe filter is your best choice.

Useful Tips and Considerations

  1. Use wisely{% set %}Tags:When you need to make complex judgments or need to refer to the judgment results multiple times, you can store the results of the filter 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,containandindexAll filters are:Case sensitive:English.This means that “CMS” and “cms” are considered different substrings.

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

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


Common Questions (FAQ)

1.containDoes the filter distinguish between uppercase and lowercase?

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

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

Of course. You can combine multiplecontainFilter is related toifTagsorlogical operators. 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 useandoperators.

3. In addition to strings,containwhat data types can filters check?

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

  • arrays (slices): to determine if a certain value exists in an array.
  • Key-value pair (map) or structure (struct): Check if a certain key name exists. For example,["Apple", "Banana"]|contain:"Apple"it will returnTruewhile{ "name":"Alice" }|contain:"name"will also returnTrue.