As an experienced website operations expert, I know that personalization and dynamic display are the key to improving user experience and increasing content conversion rates.AnQiCMS (AnQiCMS) has provided great convenience for us to achieve these goals with its flexible and powerful template engine.Today, let's delve into a very practical scenario in template making:How to check if a label is in a specific list when displaying different content dynamically based on user tags?
In AnQi CMS, we often label articles, products, and other content with various tags, such as "New Product Recommendation", "Limited Time Promotion", "VIP Exclusive", "Hot Selling Bestsellers", and so on.When we need to display different prompts, styles, or functional modules on the front-end page, such as an article list page or a product details page, we need a set of effective judgment mechanisms.This is like setting a 'qualification standard' for content: only content that matches a specific tag combination can receive special display treatment.
Understanding the template mechanism and tag acquisition in Anqi CMS
In Anqi CMS templates, we use a template engine syntax similar to Django. This means variables are enclosed in double curly braces{{变量}}However, logical control and loops use single curly braces with a percentage sign.{% 标签 %}To achieve our goal, we first need to be able to obtain the tags associated with the current document (or content).
Assuming we are on a document list page or detail page, document data and its associated tags can be obtained in the following way:
Get the document list or details:
- For lists, we usually use
{% archiveList archives ... %}tags to iterate over the document. - For detail pages, the current document data is usually available directly, or use
{% archiveDetail archive with ... %}to get the details of a specific document.
- For lists, we usually use
Retrieve the associated tags of the document:In
archiveListorarchiveDetailEach returned document (oritem), contains itsIdunique identifier. We can use this ID, through{% tagList tags with itemId=item.Id %}Tag to get the list of all tags associated with the document. ThistagsThe variable will be an array containing multiple tag objects, each withTitle(label name),Link(Tag link) and other properties.
Here, we already have two sets of data: one is the content itself, and the other is all the tags carried by the content.The key is how to determine whether one or more of these tags exist in the predefined 'specific list'.
Build a "specific list" and perform label attribution check
The Anqi CMS template engine provides powerful filter (Filter) functions that can process and convert variables. Among them,|listFilters and|containThe filter is exactly the tool to solve our current problem.
Define your "specific list":We can go through the template by
{% set %}Label collaboration|listA filter to define a string array, this is what we expect as a "specific list". For example, we want to identify the tags "VIP Exclusive" and "New Product Recommendation":{% set specialTags = '["VIP专属", "新品推荐"]'|list %}here,
specialTagsIt becomes an array containing the strings “VIP Exclusive” and “New Product Recommendation”.Utilize
|containThe filter checks the tag affiliation:|containA filter is used to determine whether a collection (such as an array) or a string contains a specific element or substring. It is used in the following way:集合变量|contain:要查找的元素.Now, we will integrate the previous steps:
”`twig {# Assume we are in a loop of document lists #} {% archiveList archives with type=“page” limit=“10” %}
{% for archive_item in archives %} <div class="article-card"> <h3><a href="{{archive_item.Link}}">{{archive_item.Title}}</a></h3> {# 定义我们想要检查的特定标签列表 #} {% set targetTags = '["VIP专属", "新品推荐", "独家"]'|list %} {# 标记,用于判断当前文档是否包含特定标签 #} {% set hasSpecialTag = false %} {# 获取当前文档的所有标签 #} {% tagList current_archive_tags with itemId=archive_item.Id %} {% for tag in current_archive_tags %} {# 检查当前标签的Title是否在targetTags列表中 #} {% if targetTags|contain:tag.Title %} {% set hasSpecialTag = true %} {# 如果找到任意一个,就设置为true #} {% endif %} {% endfor %} {% endtagList %} {# 根据hasSpecialTag的值,显示不同的内容 #} {% if hasSpecialTag %} <span style="color: red; font-weight: bold;">【{{ archive_item.Title }} - 专属推荐!】</span> {% else %} <span>{{ archive_item.Description|truncatechars:100 }}</span> {% endif %} {# 也可以在这里展示所有标签,辅助调试或作为内容的一部分 #} <div class="tags"> {% tagList archive_tags_display with itemId=archive_item.Id %} {% for tag_display in archive_tags_display %} <a href="{{tag_display.Link}}" class="tag-badge">{{tag_display.Title}}</a> {% endfor %} {% endtagList %} </div> </div> {% empty %} <p>暂无内容。</p> {% end