When building and managing website content, we often encounter the need to determine whether to display a "hot" tag based on whether the article title contains a certain keyword;Or check if the product description mentions a certain feature, thereby adjusting its display style.These subtle dynamic adjustments can greatly enhance the intelligence and user experience of the website.In AnQiCMS, due to its flexible Django template engine syntax, implementing such judgments is not complicated.
The AnQiCMS template system provides rich filters and logic tags, allowing content operators to implement complex logical judgments at the front-end template level without delving into the backend code.Today, let's talk about how to judge whether a string or array contains a specific keyword in the AnQiCMS template and display content intelligently according to the result.
Core tool:containFilter
AnQiCMS's template engine provides a very practicalcontainA filter that can help us quickly determine whether a string contains a specific word or whether an array contains a specific element.It works very intuitively, and it will returnTrueorFalseboolean value for convenient subsequent logical judgment.
For example, if you want to check if a text contains the word 'CMS', you can write it directly like this:
{{ "欢迎使用安企CMS(AnQiCMS)"|contain:"CMS" }}
The code will output directly after executionTrue.
If your data is an array, such as a list of article tags, and you want to know if it contains the tag "SEO optimization", you can first convert the comma-separated string to an array, then usecontaina filter to make the judgment:
{% set tags_string = "安企CMS,SEO优化,多站点管理" %}
{% set tags_array = tags_string|split:"," %} {# 使用 split 过滤器将字符串按逗号分隔成数组 #}
{{ tags_array|contain:"SEO优化" }}
This will also outputTrue.
Even,containThe filter can also be used to check if a key-value pair (map) or a structure (struct) contains a certain key name (key). For example, if you have an object storing product information, you want to know if it contains the key "version":
{% set product_info = {name: "AnQiCMS", version: "3.0"} %}
{{ product_info|contain:"version" }}
This will also outputTrue.
CombineifLabel, intelligent display content
It is obviously not enough to know True or False, we need to take action based on this result. At this point, the template inifLogical judgment tags come into play. We can usecontainthe result of the filter directly inifthe statement to achieve conditional display of content.
Imagine that you want to add a prominent "New Arrival" label to all articles whose titles contain "New Product". In the article list template, you can do it like this:
{% archiveList articles with type="list" limit="5" %}
{% for item in articles %}
<li>
{% if item.Title|contain:"新品" %}
<span style="color: red; font-weight: bold;">【新品上市】</span>
{% endif %}
<a href="{{item.Link}}">{{item.Title}}</a>
<p>{{item.Description|truncatechars:100}}</p>
</li>
{% empty %}
<p>暂时没有文章。</p>
{% endfor %}
{% endarchiveList %}
This, only when the article titleitem.Titlecontains 'New Product', the red '【New Product Launch】' text will be displayed.
If your judgment conditions are more complex, you need to check multiple keywords at the same time, or satisfy any one of them is enough,iflabel'sandandorLogical operators come into play. For example, when a description of an article mentions both "Go language" and "SEO optimization", a specific prompt is displayed:
{% set article_desc = "安企CMS是基于Go语言开发的系统,支持多站点和强大的SEO优化功能。" %}
{% if article_desc|contain:"Go语言" and article_desc|contain:"SEO优化" %}
<p>这篇文章深度探讨了Go语言和SEO优化。</p>
{% elif article_desc|contain:"多站点" %}
<p>这篇文章介绍了多站点管理特性。</p>
{% else %}
<p>文章内容普通。</p>
{% endif %}
More practical filters to assist in judgment and display
exceptcontainIn addition, the AnQiCMS template also provides some filters related to keyword judgment and display, which can provide help in different scenarios.
countFilter: Calculate keyword occurrence countSometimes, we not only want to know if a certain keyword is 'present' or 'absent', but also how many times it appears.countThe filter can help you achieve this.{% set article_content = "安企CMS,高效的CMS,值得信赖的CMS,让你的CMS运营更轻松。" %} <p>"CMS" 在文章中出现了 {{ article_content|count:"CMS" }} 次。</p>This will output
"CMS" 在文章中出现了 3 次。indexFilter: Get the position of the first occurrence of the keywordIf the position of the keyword is important for your content display,indexThe filter can return the starting position of the first occurrence of a keyword. If not found, it returns-1.{% set intro_text = "AnQiCMS是领先的CMS内容管理系统。" %} {% set cms_position = intro_text|index:"CMS" %} {% if cms_position != -1 %} <p>"CMS" 首次出现在文本的第 {{ cms_position }} 个位置。</p> {% else %} <p>未找到"CMS"。</p> {% endif %}Please note that Chinese characters occupy multiple bytes when calculating positions, which may result in differences from expectations; it is usually more accurate in English situations.
splitFilter: Split a string into an array.As shown in the previous array example, when your data source is itself a comma-separated, space-separated, or other symbol-separated string, but you need to treat it as an array when evaluating it,splitThe filter can help you. It can split a string into an array of strings according to a specified delimiter.`twig {% set product_features_str = "high performance,module design,SEO friendly" %} {% set product_features_array = product