In the template development of AnQi CMS, we often need to dynamically display information based on the specific attributes or keywords of the content.For example, determine if an article title contains a certain product name, or check if a document's tag list includes a popular keyword.This not only makes the website content more targeted, but also improves user experience and SEO effects.
The template engine of Anqi CMS provides powerful and flexible features to meet these needs. To determine whether an array or string contains a specific keyword, you can cleverly use the built-incontainFilter to easily implement.
UnderstandingcontainFilter
containThe filter is a very practical tool in the Anqi CMS template, which helps us determine if a value (string, array, slice in Go language, map, or struct) contains another specified value (i.e., a keyword). This filter returns a boolean value after execution: if it contains, it returnsTrueIf not included, it returnsFalse.
Its basic usage is very intuitive:
{{ obj|contain:关键词 }}
Here,objrepresents the string or array variable you want to check, while关键词is the target you want to find.
Actual application scenario: judge the keyword in the string
Suppose you want to check if the title of an AnQi CMS article contains the brand name 'AnQi CMS'.archive.TitleYou can use it in the template like this.containFilter:
{% set articleTitle = archive.Title %} {# 获取当前文章的标题 #}
{% if articleTitle|contain:"安企CMS" %}
<p>当前文章标题“{{ articleTitle }}”中包含了“安企CMS”这个词!</p>
{% else %}
<p>当前文章标题“{{ articleTitle }}”中未包含“安企CMS”。</p>
{% endif %}
In this way, you can add special tags or styles to articles containing specific brand keywords in the article list to attract users' attention. Similarly, if you want to check the content of the articles (archive.ContentDoes the text contain a specific phrase, and the same method can also be used. However, please notearchive.Contentusually contains HTML, if you need to search pure text, you may need to remove the HTML tags first (for example, usingstriptagsfilters), and then perform keyword judgment.
Actual application scenario: Determine the keywords in the array
In AnQi CMS, many data exist in the form of arrays (or slices in Go language), such as the list of tags for articles (}]}tags)。If you want to determine whether an article has been tagged with the 'marketing' label, you can do it like this:
Firstly, we go through:tagListTag retrieval gets the tag list of the article and assigns it to a variable. Then, usecontainFilter checks if the target tag is included in this list.
{% tagList currentArticleTags with itemId=archive.Id %} {# 获取当前文章的所有标签,赋值给currentArticleTags #}
{% if currentArticleTags|contain:"营销" %} {# 检查标签列表中是否包含“营销”这个字符串 #}
<p>这篇文章被标记为“营销”类别。</p>
{% else %}
<p>这篇文章没有“营销”标签。</p>
{% endif %}
It is worth noting that whencontainThe filter is used on an array, it checks if each element in the array matches the keyword you provide exactly.
Advanced Usage: Handling non-standard array data
Sometimes, the field you customize may store a string connected by commas or other delimiters, and you want to treat it as an "array" for keyword judgment. For example, you have defined a field namedrelatedKeywordsThe custom field, stored in the format "keyword1,keyword2,keyword3".
In this case, you can first usesplitThe filter splits this string into a real array first, then usescontainThe filter makes the following judgment:
{% set relatedKeywordString = archive.relatedKeywords %} {# 假设这是一个自定义字段 #}
{% set relatedKeywordArray = relatedKeywordString|split:", " %} {# 将字符串按", "拆分成数组 #}
{% if relatedKeywordArray|contain:"SEO" %}
<p>这篇文档与SEO主题高度相关。</p>
{% else %}
<p>这篇文档没有明确的SEO相关关键词。</p>
{% endif %}
Here,split:", "Splitting strings using commas and spaces as delimiters. You can adjust according to the delimiter you are actually using.splitthe parameter after the filter.
Summary
containThe filter is a simple yet powerful tool in the Anqi CMS template, which makes it very convenient to perform keyword judgment on string and array data at the template layer.Whether it is for conditional judgment, dynamic content display, or filtering based on user input, mastering this filter can make your website content operation more flexible and efficient.iflogical tags as wellsplitWith auxiliary filters, you can build more intelligent and interactive website features to better meet the needs of visitors.
Common Questions and Answers (FAQ)
Q: Can I also count the number of times a keyword appears in a string, in addition to checking if it contains it?A: Of course. The Anqi CMS template provides
countA filter to meet this requirement. You can use{{ obj|count:关键词 }}to get the number of times a keyword appears in a string or array. For example,{{"安企CMS安企CMS"|count:"CMS"}}will return2.Q: I just want to know the specific position of the keyword in the string, not just to determine if it exists. Is there a way to do that?A: Yes. You can use
indexFilter to find the position of the first occurrence of a keyword.It returns an integer representing the starting index (from 0) of the keyword in the string.-1For example,{{"欢迎使用安企CMS"|index:"CMS"}}It may return18[It depends on the actual UTF-8 encoding length of the string).Q: If the array I want to judge contains objects (such as a list of articles),
containCan the filter directly judge the value of an object's property?A:containWhen the filter is used for an array containing objects, it will try to match each object in the array with the keyword you provideExact match.If your keyword is a string, it will only check if there is an object with the exact same string in the array.ifCheck each object's specific attribute individually. For example:{% for item in articles %}{% if item.Title|contain:"某个标题" %}{# 逻辑 #}{% endif %}{% endfor %}.