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 data content.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 for functions such as permission control.
The AnQi CMS uses a template engine syntax similar to Django, providing rich tags and filters to handle data.When we receive some data collections from the backend, such as a list of article tags, a group of product images, or multiple values of a custom field, these data are often presented in the form of an array.At this point, 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 Tips: UsecontainFilter
In the AnQi CMS template system, the most direct and recommended method for checking if a specific element exists in an array is to usecontainA filter that is powerful and easy to understand, it determines whether a string, array (slice), key-value pair (map), or struct 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, the article can be associated with multiple features, and these features are stored in an array.article.FeaturesFor example,["多图", "置顶", "推荐"]Judging whether the current article contains the 'recommended' feature in the template to display a special marker.
{# 假设 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 an array of features.articleFeatures. Then,{% if articleFeatures|contain:"推荐" %}This line of code is the core judgment: it callscontainthe filter to checkarticleFeatureswhether the array contains the string “recommend”. If it contains, the condition isTrueWill display the 'Editor's Recommendation' badge; otherwise, the condition isFalseDisplay 'Normal Article'.
Another option:indexFilter
exceptcontainFilter, you can also consider usingindexFilter to achieve a similar purpose.indexThe filter returns the first occurrence position (index) of the specified keyword in a string or array, if not found, it returns-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 is1Because the array index starts from0). We go through!= -1Check if it exists. If it does, we can also make additional use ofindexThe specific position information returned by the filter, through|add:1Converted to a sequence number starting from 1 that is more in line with human habits.
AlthoughindexThe filter can also implement existence judgment, but if you just want to know if an element exists,containThe filter is semantically clearer and the code is also more concise.indexMore suitable for the scenario where you need to obtain the specific location after confirming the element exists.
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 according to the recommended attributes of the article:AnQi CMS article details (
archiveDetailThere usually isFlagA field is used to mark the recommended attributes of an article, such ashrepresenting the headline,cThis represents a recommendationFlagIt may be a string containing multiple letters, such as"hc". We can usecontainFilter it 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 processing for the product detail page image group:Assuming your product model has a
Imagesfield, stores the URL array of product galleries. You may want to check if it contains a specific promotional image for special display on the page.“`twig {% archiveDetail productDetail with name=“Images