In AnQiCMS templates, dynamically determining whether a string contains a specific keyword is a very practical feature, which can help us implement various intelligent content display and interaction logic on the website front-end.For example, you can display different icons based on whether the article title contains a specific word, or highlight some key information in the product description.
AnQiCMS uses a template engine syntax similar to Django, providing rich tags and filters for data processing. To judge whether a string contains a specific keyword in the template, we can take advantage of a very convenient built-in filtercontain.
Core Tool:containFilter
containThe filter is a powerful tool in the AnQiCMS template system for detecting the existence of keywords.Its main function is to determine whether a target string, array, key-value pair (map), or structure (struct) contains the keyword you specify.TrueorFalse), clearly tells you whether the keyword exists.
Basic usage
containThe usage of the filter is very intuitive, usually with a pipe symbol|After connecting to the target variable, and passing in the keyword you want to check:
{{ 目标变量 | contain:"关键词" }}
For example, if we want to judge whether a variable namedarticleTitle(representing the article title) contains the keyword “AnQiCMS”, we can write it as:
{{ articleTitle | contain:"AnQiCMS" }}
IfarticleTitleThe value is "Welcome to AnQiCMS content management system", so the output of the above code will beTrue.
Actual application: judgment and conditional rendering in the template
UnderstoodcontainAfter the basic function of the filter, the most common application scenario is to combine conditional judgment tags{% if %}To achieve dynamic content display.
Assuming we want to display a 'Newcomer Tutorial' badge in the article title if it contains the words 'Tutorial', we can implement it like this:
{% set articleTitle = archive.Title %} {# 假设 archive.Title 是当前文章的标题 #}
<div class="article-header">
<h1>{{ articleTitle }}</h1>
{% if articleTitle | contain:"教程" %}
<span class="badge badge-primary">新手教程</span>
{% endif %}
</div>
In this example:
- We first use
{% set %}The tag assigns the article title to a variable namedarticleTitle, which helps improve the clarity and maintainability of the code. - Next, in
{% if %}In the statement, we appliedarticleTitle | contain:"教程"to determine if the title contains 'tutorial'. - If the judgment result is
True, then<span>新手教程</span>this part of the content will be rendered on the page; if it isFalsethis part of the content will not be displayed.
More scenarios: judgment of arrays and complex data structures
containThe power of the filter lies not only in strings, but also in arrays, key-value pairs, and structures.
1. Determine if a value exists in the array
If you have a list of keywords (for example, a list of article tagsarchive.Tags), and you want to check if it contains a specific tag,containthe filter can also be useful.
{% set articleTags = archive.Tags %} {# 假设 archive.Tags 是一个包含多个标签的数组,如 ["CMS", "模板", "教程"] #}
<div class="article-tags">
标签:
{% for tag in articleTags %}
<span class="tag">{{ tag }}</span>
{% endfor %}
{% if articleTags | contain:"模板" %}
<p>这篇文章与“模板”相关,可能对设计师有帮助!</p>
{% endif %}
</div>
Here, articleTags | contain:"模板"it will check the arrayarticleTagsWhether the element contains the string "template".
2. Determine whether a key-value pair (Map) or a structure (Struct) contains a certain key name.
When dealing with some custom fields or complex data structures, you may need to determine if a key name (key) exists.containThe filter can also meet this need.
Suppose we have a namedcustomFieldsThe object, which may contain some custom settings, we want to check if it hasspecialOfferThis key:
{% set customFields = archive.CustomFields %} {# 假设 customFields 可能是一个包含多个自定义键值对的对象 #}
{% if customFields | contain:"specialOffer" %}
<p>该产品目前有特别优惠!</p>
{% else %}
<p>该产品暂无特别优惠。</p>
{% endif %}
HerecustomFields | contain:"specialOffer"Will checkcustomFieldsWhether the object contains a namedspecialOfferkey?
Flexible use to enhance template expression
BycontainThe filter allows us to easily implement intelligent keyword detection in the AnQiCMS template, making the display of website content more flexible and personalized.Whether it is for conditional rendering, data filtering, or simple content verification, mastering this filter can greatly enhance the performance and maintainability of your template.{% set %}Variable assignment and{% if %}Conditional judgment can build clear and powerful template logic.
Frequently Asked Questions (FAQ)
1.containIs the filter case sensitive?
Yes,containThe filter is case sensitive.This means that "AnQiCMS" and "anqicms" are considered different keywords.If you need to perform a case-insensitive comparison, consider converting the target string and keywords to a uniform case (such as all lowercase) before comparison.lowerandupperFilters to help you complete case conversion.
2. BesidescontainAre there other similar features that can be used to process strings?
Of course. The AnQiCMS template system also provides some other filters related to string processing, such as:
index: Returns the first occurrence position of the keyword in a string or array, or -1 if not found.count: Calculates the number of times the keyword appears in a string or array.replaceTranslate a keyword in a string to another keyword.cutRemove the specified character from a string.\nThese filters can be used according to specific requirements, andcontainUsing filters together, it can achieve more complex string operations.
3.containCan the filter determine if the entire content of the article contains a certain word?
Can be. If the content of your article is stored in a variable (for examplearchive.Content), you can directly use this variable withcontaina filter to determine if it contains specific keywords. For example:{% if archive.Content | contain:"重要通知" %}This is very useful when it is necessary to filter the content of the article or display a content summary on the page.