In Anqi CMS template development, we often need to dynamically display information based on specific attributes or keywords of the content.For example, determine whether an article title contains a product name or check if a document's tag list contains a popular keyword.This not only makes the website content more targeted, but also improves user experience and SEO effectiveness.

The AnQi CMS template engine provides powerful and flexible features to meet these needs. Among them, determining whether an array or string contains a specific keyword can be achieved by cleverly using built-incontainImplement filters easily.

UnderstandingcontainFilter

containThe filter is a very practical tool in the Anq CMS template, which helps us determine whether a value (string, array, slice in Go language, map or struct) contains another specified value (i.e., the keyword). After execution, this filter returns a boolean value: if it contains, it returnsTrue; if it does not contain, it will returnFalse.

Its basic usage is very intuitive:

{{ obj|contain:关键词 }}

Here, objrepresent the string or array variable you want to check, and关键词is the target you want to find.

Actual application scenario: Determine the keywords in the string

Assuming you want to check the title of an Anqi CMS article (archive.Title) to see if it contains the brand name 'Anqi CMS'. You can use it in the template like thiscontainFilter:

{% 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 in the list that contain specific brand keywords to attract the attention of users. Similarly, if you want to check the content of the articles (archive.Content) Is there a specific phrase, you can also use the same method. However, please notearchive.ContentIt usually contains HTML, if you need to search for plain text, you may need to remove the HTML tags first (for example usingstriptagsfilter), and then perform keyword judgment.

The actual application scenario: to judge the keywords in the array

In AnQi CMS, many data exist in the form of arrays (or slices in Go language), such as the tag list of articles (tagsIf you want to determine whether an article has been tagged with "marketing", you can do it like this:

First, wetagListTagging to get the tag list of the article and assign it to a variable. Then, usecontainFilter to check if the list contains the target tag.

{% 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.

Advanced usage: Handling non-standard array data.

Sometimes, the custom fields you define 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 define a field namedrelatedKeywordsThe custom field stores the format as "keyword1, keyword2, keyword3".

In this case, you can first usesplitThe filter splits this string into a real array and then uses itcontaina filter to make the judgment:

{% set relatedKeywordString = archive.relatedKeywords %} {# 假设这是一个自定义字段 #}
{% set relatedKeywordArray = relatedKeywordString|split:", " %} {# 将字符串按", "拆分成数组 #}

{% if relatedKeywordArray|contain:"SEO" %}
    <p>这篇文档与SEO主题高度相关。</p>
{% else %}
    <p>这篇文档没有明确的SEO相关关键词。</p>
{% endif %}

here,split:", "Split strings with a comma and space as the delimiter. You can adjust according to the delimiter you are actually using.splitThe parameters after the filter.

Summary

containThe filter is a simple yet powerful tool in Anqi CMS template, which makes it very convenient to perform keyword judgment on string and array data at the template level.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.Through combiningifLogical labels andsplitAn auxiliary filter, you can build more intelligent and interactive website features to better meet the needs of visitors.


Frequently Asked Questions (FAQ)

  1. Q: Can I also count how many times a keyword appears in a string, in addition to checking if it contains it?A: Of course. Anqi CMS template providescountA 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"}}it will return2.

  2. Q: I just want to know the specific position of the keyword in the string, not just to judge if it exists. Is there a way to do that?A: Yes. You can useindexA filter to find the first occurrence of a keyword. It returns an integer representing the starting index of the keyword in the string (counting from 0).If the keyword is not found, it will return-1For example,{{"欢迎使用安企CMS"|index:"CMS"}}May return18The length of the string depends on the actual UTF-8 encoding.

  3. Q: If the array I need to judge contains objects (such as a list of articles),containCan the filter directly judge the value of an object's attribute?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 providePerfect matchIf your keyword is a string, it will only check if there is an object exactly the same as the string in the array.To determine the properties of an object (for example, to determine whether there is an article with a Title of "some title" in a list of article objects), you need to traverse the array and use it in the loop.ifCheck each object's specific attribute individually. For example:{% for item in articles %}{% if item.Title|contain:"某个标题" %}{# 逻辑 #}{% endif %}{% endfor %}.