In the daily operation and template customization of Anqi CMS, we often encounter situations where we need to dynamically adjust the page display based on the content of the data.One common and practical requirement is to determine whether a specific element exists within an array (or list) in a template.This is crucial for implementing personalized recommendations, conditional content display, and even permission control functions.
The Anqi CMS uses a template engine syntax similar to Django, providing rich tags and filters for data processing.When we retrieve some data collections from the backend, such as a list of article tags, a group of product images, or multi-select values for a custom field, these data are often presented in the form of an array.If we need to decide what content to display based on whether an array contains a certain value, we can cleverly use built-in filters to achieve this.
Core Skill: UsingcontainFilter
In the template system of AnQi CMS, the most direct and recommended method for checking if a specific element exists within an array is to usecontainFilter. This filter is powerful and easy to understand, it checks if a string, array (slice), key-value pair (map), or structure contains the specified keyword or key name, and returns a boolean value (TrueorFalse).
Let's look at a simple example. Suppose you have a custom article feature, which can be associated with multiple characteristics, these characteristics are stored in an array.article.Featuresfor example["多图", "置顶", "推荐"]You want to determine if the current article contains the 'recommended' feature in the template so that a special mark can be displayed.
{# 假设 article 是当前文章对象,其 Features 是一个字符串数组 #}
{% set articleFeatures = '["多图", "置顶", "推荐"]'|list %}
{% if articleFeatures|contain:"推荐" %}
<span class="badge badge-primary">编辑推荐</span>
{% else %}
<span>普通文章</span>
{% endif %}
In this code, we first usesetandlistThe filter simulates a feature arrayarticleFeatures. Then,{% if articleFeatures|contain:"推荐" %}This line of code is the core of the judgment: it callscontainthe filter to checkarticleFeatureswhether the array contains the string "recommended". If it does, the condition isTruewill display the "Edit Recommended" badge; otherwise, the condition isFalsewill display "Normal Article".
Another option:indexFilter
Exceptcontainfilter, you can also consider usingindexfilter to achieve a similar purpose.indexThe filter will return the position (index) of the first occurrence of the specified keyword in a string or array, if not found, it will return-1. Therefore, by checking if the returned value is not equal to-1We can also confirm the existence of the element.
{% set articleFeatures = '["多图", "置顶", "推荐"]'|list %}
{% if articleFeatures|index:"置顶" != -1 %}
<span class="badge badge-warning">置顶文章,位于第 {{ articleFeatures|index:"置顶"|add:1 }} 位</span>
{% else %}
<span>未置顶</span>
{% endif %}
In this example,articleFeatures|index:"置顶"It will return the index of 'top' in the array (here)1because the array index starts from0). We use!= -1Check if it exists. If it does, we can also take advantage of it extra.indexThe specific location information returned by the filter, through.|add:1Converted to a serial number starting from 1, which is more in line with human habits.
AlthoughindexThe filter can also perform existence checks, but if you just want to know if an element exists,containthe filter's semantics are clearer, and the code is also more concise.indexMore suitable for the scenario where you need to get the specific position of an element after confirming its existence.
Example of practical application scenarios
Let's combine the commonly used tags of AnQi CMS and see how these filters are applied in actual templates:
Display icons based on the recommended attributes of the article:AnQi CMS article details (
archiveDetailIn parentheses there is usually aFlagfield, used to mark the recommendation attributes of an article, such ashrepresents the headlinecrepresents the recommendation. ThisFlagmay be a string containing multiple letters, such as"hc"We can usecontaina filter to check it.{% archiveDetail currentArticle %} <h1 class="article-title">{{ currentArticle.Title }}</h1> {% if currentArticle.Flag|contain:"h" %} <img src="/static/images/hot.png" alt="头条文章" title="头条文章" class="flag-icon"> {% endif %} {% if currentArticle.Flag|contain:"c" %} <img src="/static/images/recommend.png" alt="推荐文章" title="推荐文章" class="flag-icon"> {% endif %} <div class="article-content">{{ currentArticle.Content|safe }}</div>Special treatment for the product detail page image group:Assuming your product model has
ImagesField, stores the URL array of product image galleries.You may want to check if it contains a specific promotional image for special display on the page.