How to determine if a string or array contains specific content in AnqiCMS?

Calendar 👁️ 72

In the daily work of content operation, we often need to judge and display according to the specific information of the content.For example, if a blog post mentions a hot keyword, we may want to give it a special tag;Or a product description includes some feature, we want to adjust its display style accordingly.The template engine of AnQiCMS (AnQiCMS) provides a flexible and intuitive way to determine whether a string or array contains specific content, making dynamic content display and personalized processing very convenient.

To achieve such judgment, Anqi CMS mainly provides two powerful mechanisms:containFilters andinoperators, each of which is suitable for different scenarios.

Core tool:containFilter

containThe filter is a tool specifically used in Anqi CMS templates to check if content contains specific keywords.Its strength lies in its ability to be used not only for strings, but also to effectively check for the existence of specified content in arrays, maps (map), and even structs (struct)When it finds a match, it will return a boolean value (TrueorFalseThis makes it very suitable for use with conditional judgments.

containThe basic usage of the filter is{{对象 | contain: "关键词"}}.

1. Determine if the string contains the keyword:

This is the most common application scenario. If you have a piece of text, you want to know if it mentions "AnQi CMS":

{% set article_content = "欢迎使用安企CMS(AnQiCMS)来搭建您的网站。" %}
{% if article_content | contain: "安企CMS" %}
    <p>文章中提到了安企CMS!</p>
{% else %}
    <p>文章中没有提及安企CMS。</p>
{% endif %}

Ifarticle_contentThe value of the variable is “Welcome to use AnQiCMS (AnQiCMS) to build your website.”, then the above code will output “AnQiCMS is mentioned in the article!”.

2. Check if an element exists in an array:

If your variable is an array (usually in Go language templates, it isslice)containThe filter can check if there is an element in the array that matches the keyword exactly.

{% set keywords_list = ["安企CMS", "网站建设", "内容管理"] %}
{% if keywords_list | contain: "网站建设" %}
    <p>关键词列表中包含“网站建设”。</p>
{% else %}
    <p>关键词列表中不包含“网站建设”。</p>
{% endif %}

Here, "website construction" is an element in the array, so it will output "The keyword list includes 'website construction'.". It is noteworthy that,containThe filter performs elementsPerfect match.

3. To determine if a key exists in a map (Map) or struct (Struct):

containFilters can also be used to check if a specific key name (field name) exists in a map or structure. This is very useful for checking if a certain attribute exists in a data structure.

{% set product_info = {"name": "安企CMS", "version": "3.0", "price": 0} %}
{% if product_info | contain: "price" %}
    <p>产品信息中包含价格字段。</p>
{% else %}
    <p>产品信息中不包含价格字段。</p>
{% endif %}

The code will output “The product information contains a price field.” because it checks whetherpricethe key name exists.

a more general judgment method:inOperator

exceptcontainFilter, the Anqi CMS template engine also supportsinoperator, which is usually used directly forifIn the statement, determine whether a value is in another collection (such as a string or array).inThe operator provides a more concise syntax, especially for direct condition judgments.

1. Check if a string contains a substring:

withcontainSimilar filter,inThe operator can directly check if one string contains another substring.

{% set page_title = "关于安企CMS的优势" %}
{% if "CMS" in page_title %}
    <p>页面标题中包含“CMS”。</p>
{% else %}
    <p>页面标题中不包含“CMS”。</p>
{% endif %}

2. Check if an element is in an array:

inThe operator can also determine whether an element exists in an array.

{% set tag_names = ["建站", "营销", "优化"] %}
{% if "优化" in tag_names %}
    <p>这个标签在列表中。</p>
{% else %}
    <p>这个标签不在列表中。</p>
{% endif %}

inOperator andcontainFilter selection:

  • If you just want toifMake a simple 'contains' judgment in the statement,inOperators are usually more concise and intuitive.
  • If you need to store the result of a judgment in a variable or chain it with other filters, thencontainThe filter will be a better choice because it returns a boolean value.

It includes not only: digging deeper into the content

In some cases, you may not only want to know whether it contains, but also need more detailed information, such as the position or frequency of the keyword.AnQi CMS also provides the corresponding filters to meet these needs.

1.indexFilter: locate the position of keyword occurrence

If you want to know not only whether it contains but also the position of the first occurrence of the keyword, you can useindexA filter. It returns the index of the first occurrence of the keyword (starting from 0), or returns 0 if not found-1. This can be used as another way to judge whether it contains (i.e.index >= 0)

{% set document_text = "安企CMS是一个高效的内容管理系统,CMS功能强大。" %}
{% set first_cms_pos = document_text | index: "CMS" %}
{% if first_cms_pos >= 0 %}
    <p>“CMS”首次出现在位置:{{ first_cms_pos }}</p>
{% else %}
    <p>未找到“CMS”。</p>
{% endif %}

It should be noted that for strings containing Chinese,indexThe filter calculates the position by treating a Chinese character as multiple byte lengths (a Chinese character usually occupies 3 bytes under UTF-8 encoding).

2.countFilter: Count the number of times a keyword appears

If you want to know how many times a keyword appears in a string or array, you can usecountfilter.

{% set long_text = "安企CMS提供卓越的用户体验,安企CMS致力于服务中小企业。" %}
{% set cms_count = long_text | count: "安企CMS" %}
<p>“安企CMS”在文本中出现了 {{ cms_count }} 次。</p>

Similarly, for arrays,countThe filter counts the number of times elements match completely.

Actual application scenario: Make content smarter

  • Conditionally display a specific block:If the article content contains the word 'video', a video player area will be displayed.
  • Highlight keywords:Although highlighting directly requires more complex string replacement logic, by checking for the existence of keywords, special styles can be added to pages or list items that contain specific keywords.
  • Adjust SEO information according to content:Although TDK tags are usually set in the background, in some advanced template scenarios, it can be determined whether to adjust auxiliary Meta tags based on the dynamic content of the page.
  • Form validation information:In some custom forms, you can check if the user input contains sensitive words or other specific patterns.

In summary, Anqi CMS provides a variety of easy-to-use template functions for determining whether a string or array contains specific content.Whether it is a simple boolean judgment or the need for more precise location or frequency statistics, it can be flexibly implemented at the template level to help content operators more efficiently control and display website content.


Frequently Asked Questions (FAQ)

Q1:inAnd operatorscontainWhat are the core differences in the usage of filters?

A1:inThe operator is directly used in the Anqi CMS template toifthe logical judgment operator of statements, for example{% if "关键词" in 变量 %}, it cannot be chained, and cannot assign the judgment result to a new variable. AndcontainA filter is a functional operation that returns a Boolean value, which can be chained with other filters or used directly{% set is_contained = 变量 | contain: "关键词" %}This way assigns the result to a new variable for use in subsequent template logic. In simple terms,inis more suitable for direct conditional judgment,containIt is more suitable for scenarios where you need to obtain boolean results and proceed further.

Q2:containCan the filter determine whether an element in the arraysectioncontains? For example, to judge["Apple", "Banana"]if there is an element containing “an”?

Related articles

How to convert an array to a string with a delimiter, or split a string into an array?

In AnQi CMS template development, we often encounter situations where we need to handle data format conversion, especially when combining multiple values into a string or splitting a string containing multiple values into independent data.This is especially common when dealing with tags, multi-select properties, or extracting information from custom fields. 幸运的是,AnQi CMS built-in very practical template filters, can help us easily achieve the conversion between arrays and string with delimiters.

2025-11-08

How to perform simple number or string concatenation in AnqiCMS template?

In Anqi CMS template design, flexibly handling and displaying data is the key to building a dynamic website.This is a common need in daily development to concatenate numbers or strings.AnQi CMS provides various ways to achieve this goal, from simple direct combinations to powerful filters, which can meet your different content assembly scenarios.Understanding the Basics of Concatenation: Direct Output Equivalent to "Addition" In Anqi CMS template syntax, the most intuitive way to concatenate strings is to place multiple variables or text directly side by side. For example

2025-11-08

How to remove leading and trailing spaces or specified characters from a string?

In website content management, string formatting is often a problem we need to solve when displaying content and handling data.Especially in user input, data import, or system-generated content, strings may have extraneous spaces at the beginning and end, or specific characters may need to be removed to meet expected display effects or data standards.AnQiCMS as an efficient and flexible content management system, through its powerful template engine and rich filters, provides us with concise and powerful string processing capabilities, allowing such tasks to be easily completed at the template level.

2025-11-08

How to format numbers in Anqi CMS (such as retaining decimal places for floating-point numbers)?

In website operation, the way data is displayed is crucial to user experience.Whether it is product prices, statistics, or financial statements, clear and standardized number formats can not only enhance the professionalism of the website but also help users understand information more intuitively.AnQi CMS knows this need, and provides powerful number formatting functions in the template system, allowing us to easily control the decimal places of floating-point numbers, the display style of integers, and more customized presentations of numbers.### Why is Number Formatting So Important? Imagine a e-commerce website where the price of a product is displayed as "199"

2025-11-08

How to debug and view the complete structure and value of variables in the template?

When developing website templates in AnQi CMS, we often need to understand the specific structure of a variable on the page and what data it contains so that we can accurately call its properties.When dealing with complex business logic or unfamiliar template variables, being able to quickly view the complete structure and value of variables is undoubtedly the key to improving development efficiency.The Anqi CMS based on the Django template engine provides a concise and powerful variable debugging method for us.### Core Tool: `dump` Filter An enterprise CMS template provides a `dump` named

2025-11-08

How to display the comment form and submit comments in Anqi CMS?

It is crucial to establish an effective communication channel with visitors in website operation, and the message function is one of the key tools to achieve this goal.It can not only help websites collect user feedback and answer questions, but also promote user participation, enhance the interactivity and stickiness of the website.For users using AnQiCMS, the built-in message function provides a convenient and flexible solution, allowing you to easily display and submit message forms without writing complex code.### Overview of AnQi CMS Message Function The AnQi CMS message function is designed to be very user-friendly

2025-11-08

How to display the document comment list and implement pagination?

When using Anqi CMS to manage website content, the comment function on the document detail page is an important part of interacting with readers.Effectively display these comments and ensure that the page maintains good loading speed and user experience as the number of comments increases, it is necessary to properly handle the display and pagination of the comment list.The Anqi CMS provides very intuitive and powerful template tags, helping us easily achieve this goal.

2025-11-08

How to implement the like function for comments in AnQi CMS?

In website operation, enhancing user interaction is a key factor in maintaining the vitality of the website and increasing user stickiness.The comment section is an important battlefield for users to express their opinions and interact with each other. Adding a like feature would undoubtedly significantly enhance the interactivity and community atmosphere of the content.The Anqi CMS is an efficient, customizable, and feature-rich enterprise-level content management system that provides flexible mechanisms, allowing users to easily add the popular interactive feature of liking comments in the comment section.This article will give a detailed explanation of how to implement the like function for comments in Anqi CMS.

2025-11-08