How to judge whether a line of text in the AnQiCMS template contains a certain keyword?

Calendar 👁️ 60

In website operation, we often encounter such needs: to decide how to display page elements based on whether the content contains a specific word, such as highlighting some text, or only displaying a module when certain conditions are met.The AnQiCMS template system provides flexible filter functions to help us easily implement these keyword-based judgments.This article will delve into how to efficiently and accurately determine whether a line of text in the AnQiCMS template contains a specific keyword.

UsecontainFilter for keyword judgment

The most direct and recommended method is to usecontainThe filter is used specifically to determine whether a string, an array (slice), or even a key-value pair (map) or a struct exists with the specified keyword or key name.It returns a boolean value (TrueorFalseIt is very suitable for conditional judgment with the template{% if %}tag usage

Assuming we have a variablepageContentStoring the main content of the web page, we want to know if this content mentions the word "AnQi CMS". We can use it like thiscontainFilter:

{% set pageContent = "欢迎使用安企CMS(AnQiCMS),这是一款强大的内容管理系统。" %}
{% if pageContent|contain:"安企CMS" %}
    <p>恭喜!内容中包含关键词“安企CMS”!</p>
{% else %}
    <p>抱歉,内容中未找到关键词“安企CMS”。</p>
{% endif %}

In the above example, ifpageContentIf the string contains “AnQi CMS”, then it will display the phrase “Congratulations!”.containThe strength of the filter lies not only in its ability to judge strings, but also in its support for various data types.

1. Determine if the string contains the keyword:This is the most common usage, for example, to check an article

Related articles

What are the practices when using the `count` filter to calculate keyword density?

In website content operation, the reasonable use of keywords is an important factor in improving search engine visibility.We not only need to ensure that the core keywords are reflected in the content, but also need to pay attention to their frequency of occurrence, avoiding overloading or underusing.AnQiCMS provides a variety of powerful content management tools, among which the `count` filter is a very practical feature that can help us effectively count the number of occurrences of keywords in articles.###

2025-11-08

How to calculate the total number of occurrences of a specific value in an array in AnQiCMS?

In website operation, we often need to handle various data, one of the common needs is to count the number of times a specific value appears in a data set.For example, you may want to know how many times a keyword appears in an article, or how many colors of a product are marked as 'hot'.For users of AnQiCMS, the system's powerful template engine provides a very convenient tool to complete this task.The AnQi CMS template engine is powerful and flexible, it comes with a variety of filters (Filters)

2025-11-08

How to count the frequency of a keyword in a string in the AnQiCMS template?

In content operation, we often need to process and analyze text.This may involve checking keyword density to optimize search engine rankings, or it may just be to understand the frequency of a specific word appearing in the content in order to better assess the focus of the content.AnQiCMS (AnQiCMS) boasts its high efficiency and customizable features, providing users with powerful content management capabilities, and its flexible template system makes it easy for us to handle various content display needs.Today, let's talk about a very practical feature: how to count the number of times a keyword appears in a string in a template

2025-11-08

How to utilize the multi-site feature of Anqi CMS to achieve unified or differentiated display of content between different sites?

The multi-site feature of AnQi CMS is an extremely powerful feature, which provides unprecedented flexibility for website operators, whether it is managing multiple independent brand websites or building a content-sharing matrix, they can handle it with ease.The core of this feature lies in the fact that it allows you to efficiently manage and display multiple independently operated websites under a single secure CMS system, and cleverly balances content differentiation and unity.### Understanding the multi-site capability of AnQi CMS Firstly, we need to clarify the operation mode of the multi-site function of AnQi CMS

2025-11-08

How to check if a specific element exists in an array within a template

In the daily operation and template customization of AnQi CMS, we often encounter situations where we need to dynamically adjust the display of the page according to the data content.One common and practical requirement is to determine whether a specific element exists in an array (or list) within a template.This is crucial for realizing personalized recommendations, conditional content display, and even for functions such as permission control.The Anqi CMS adopts a template engine syntax similar to Django, providing rich tags and filters to handle data.When we get some data set from the backend, such as the list of article tags

2025-11-08

What role can the `contain` filter play in the context of sensitive word filtering?

In today's era where content is king, website operators are facing the dual challenge of actively publishing high-quality content while strictly controlling content security and compliance.Especially on platforms where user-generated content is increasing, how to efficiently and accurately filter sensitive words has become a key link in maintaining a healthy website ecosystem and protecting brand reputation.AnQiCMS (AnQiCMS) fully understands this need and provides many content security management functions, among which the `contain` filter can play a surprisingly practical role in the sensitive word filtering scenario.### The Foundation of Content Security

2025-11-08

How to get the first occurrence position of a keyword in a string in AnQiCMS

In content operation, we often need to fine-tune and manage the text content on the website.In order to content review, dynamic display, or SEO optimization, sometimes we need to know the position of the first occurrence of a specific keyword in a text.AnQiCMS (AnQiCMS) is an efficient content management system that provides a convenient way to help us meet this need.Understanding the need: Why do we need to find the position of keywords?Understanding the position of keywords in strings is of great practical value in the process of content publishing and maintenance

2025-11-08

What is the speciality of the `index` filter for Chinese position calculation when processing multi-language content?

## A clever approach: The unique features of `index` filter and Chinese position calculation in AnQi CMS multilingual content When managing multilingual content with AnQi CMS, the flexible use of its powerful template engine and rich filters can greatly improve content operation efficiency.Among them, the `index` filter is a very practical tool that helps us quickly locate the first occurrence of a specific substring in a string.However, when our content involves Chinese characters, the `index` filter has some unique behavior in position calculation.

2025-11-08