In the template development and daily content operation of AnQi CMS, we often encounter the need to dynamically display information based on the specific attributes or keywords of the content.Whether it is to achieve personalized content recommendation, emphasize the specific theme of the article, or control the display of page elements based on certain signs in the data, accurately judging whether a string or array contains a specific keyword is a very practical skill.
The template system of Anqi CMS uses a syntax similar to Django template engine, with built-in rich filters and tags, which can help us easily implement such judgments.Next, we will delve into how to determine if a string or array contains a specific keyword in the AnQi CMS template, and provide some practical methods and scenario examples.
UsecontainKeyword judgment by filter
in the Anqi CMS template, the most direct and most commonly used method is to use the built-incontainFilter. This filter is specifically used to check if a string or array (slice) contains a specified keyword. Its return value is of boolean type (TrueorFalse),非常适合与条件判断标签{% if %}结合使用。
Basic usage:
{{ obj|contain:关键词 }}
Among them,obj可以是一个字符串变量,也可以是一个数组变量,而关键词则是你想要查找的特定字符串。
Example 1: Determine if a string contains a keyword.
Assuming we have gone througharchiveDetailLabel to get the title of an article.archive.Title, and we want to determine if it contains the word 'Tutorial':
{% set articleTitle = archive.Title %} {# 假设 archive.Title 的值为 "安企CMS模板制作教程" #}
{% if articleTitle|contain:"教程" %}
<p>这篇文章标题中包含了“教程”字样,这可能是一个入门指导!</p>
{% else %}
<p>这篇文章标题中未包含“教程”字样。</p>
{% endif %}
In the above example, ifarticleTitleThe content is 'Anqi CMS template creation tutorial', then the conditionarticleTitle|contain:"教程"it will returnTrueand display the corresponding prompt information.
Example two: Determine if an array contains a specific element
Sometimes, data may exist in an array form, such as a list of Tag tags in an article. We cantagListTag acquisition Tag list, then judge whether a specific Tag exists:
{% tagList tags with itemId=archive.Id %} {# 假设 tags 数组包含 ["网站优化", "SEO", "内容营销"] #}
{% if tags|contain:"SEO" %}
<span class="highlight-tag">本文专注于SEO优化</span>
{% else %}
<span>本文未涉及SEO优化</span>
{% endif %}
{% endtagList %}
In this scenario,tagsA variable is an array containing multiple strings.tags|contain:"SEO"Check if there is an element with the value 'SEO' in the array.
Example three: Determine if a key-value pair (Map/Struct) contains a specific key name
containFilters can also be used to determine if a specific key (key) exists in key-value pairs (or structures).This is very useful when handling background custom fields, or some configuration data.
{% set extraParams = archive.Extra %} {# 假设 extraParams 是一个包含自定义字段的Map,如 { "author": "张三", "source": "官网" } #}
{% if extraParams|contain:"author" %}
<p>作者:{{ extraParams.author }}</p>
{% else %}
<p>未指定作者信息。</p>
{% endif %}
Here,extraParams|contain:"author"will checkextraParamsif there is a namedauthorThe key.
Other methods to judge keywords:indexandcountFilter
ExceptcontainOutside the filter,indexandcountThe filter can also indirectly help us judge whether a string or array contains specific keywords and provide more detailed information.
1.indexFilter: Get the position of keyword occurrence
indexThe filter returns the position (index) of the first occurrence of the keyword in a string or array, if not found, it returns-1We can use this feature to determine if a keyword exists.
Basic usage:
{{ obj|index:关键词 }}
Example: CombineindexJudgment
{% set articleContent = archive.Content %} {# 假设内容中包含 "安企CMS是一款高效的CMS系统" #}
{% if articleContent|index:"高效" > -1 %}
<p>内容中提到了“高效”,首次出现在第 {{ articleContent|index:"高效" }} 个字符处。</p>
{% else %}
<p>内容中未提及“高效”。</p>
{% endif %}
IfindexThe returned value is greater than-1This means that the keyword exists. This method is suitable for the scenario where you are not only concerned about the existence of the keyword, but also the position of its first occurrence.
2.countFilter: Calculate the number of times a keyword appears
countThe filter can count the number of times a keyword appears in a string or an array. If the count is greater than0, it indicates that the keyword exists.
Basic usage:
{{ obj|count:关键词 }}
Example: CombinecountJudgment
{% set articleContent = archive.Content %} {# 假设内容中包含 "安企CMS是一款高效的CMS系统,安企CMS致力于提供高效解决方案。" #}
{% set cmsCount = articleContent|count:"CMS" %}
{% if cmsCount > 0 %}
<p>内容中提到了“CMS”共 {{ cmsCount }} 次。</p>
{% else %}
<p>内容中未提及“CMS”。</p>
{% endif %}
WhencountThe number returned by the filter is greater than0When, it indicates that the keyword exists. This method is very useful when it is necessary to understand the frequency of keyword occurrence.
Actual application scenarios: Make content more intelligent and expressive
These keywords judgment functions have wide applications in actual website operation and template design:
- Dynamically display recommended icons or badges:For example, if the product title contains "New" or "Special Offer
- Personalized content guidance:Based on the article content the user is browsing (for example, if the article title or keywords include "Go language"), recommend more tutorials or related products about Go language in the sidebar.
- Template style control:If the body of the article contains certain specific phrases (such as "Click here to download"), special CSS styles can be applied to these phrases through judgment to make them more prominent.
- List content filtering and grouping:In the custom list in the background, by judging whether a custom field contains a specific value, to conditionally filter or group the list items displayed on the front end.
- The precise placement of advertisements or marketing information:Dynamically insert relevant advertisements or promotional information based on page theme or content keywords, to improve conversion rates.
Details to note
When using these filters, there are several details that are worth noting:
- Case sensitive:By default,
contain/indexandcountAll filters are:Case sensitive:English.This means that “CMS” and “cms” are considered as different keywords.{% if articleTitle|lower|contain:"cms" %}. - Data source:Ensure that the data (string or array) you provide to the filter is correct. Usually, these data come from various tags of the security CMS such as
archiveDetailreturnedarchive.Title/archive.Content, orarchiveListIn the loopitem.Keywordsetc. - Performance considerations:For most websites and content volume, use these in templates