In website operation, we often hope that the content can be presented more intelligently to visitors.For example, if a technical article mentions a specific product, we may want to automatically display the product recommendation information at the top or side of the article; if the product description contains the word 'Limited Edition', we would like to add an eye-catching 'Limited' badge next to the product image.This ability to conditionally display elements based on whether the content contains specific keywords can greatly enhance the user experience and operational efficiency of a website.
AnQiCMS as an efficient and flexible content management system fully considers the needs of this kind of dynamic content display.It has a powerful template engine combined with rich built-in tags and filters, providing a convenient way to achieve this intelligent content presentation.
Why do we need to judge the display based on keywords?
Imagine that if you operate an e-commerce website, products described as 'Pre-Sale' may require a countdown module to be displayed on the page; if you are a self-media operator, perhaps you would like to add a red corner mark below the title of the hot news you publish.Certainly, manually performing these operations for each article or product will undoubtedly consume a significant amount of time and effort, and is prone to errors.Through the keyword judgment function of AnQiCMS template, these elements can be displayed or hidden automatically according to the content, which not only improves efficiency but also ensures the consistency of display.It makes the website content more 'vivid', able to adjust the presentation style automatically according to the subtle features of the content, thus better attracting users' attention and guiding user behavior.
AnQiCMS template basic conditional judgment:ifTags and powerful filters
AnQiCMS's template system adopts syntax similar to Django template engine, with the core advantage of intuitive conditional judgment logic and a variety of filters. To implement displaying elements based on whether the content contains a specific keyword, the following key parts will be mainly used:
FirstlyifLogic judgment label. It is the basis for all conditional displays, allowing us to determine whether the code block in the template is executed based on the truth or falsity of an expression. Its basic structure is{% if 条件 %} ... {% endif %}English can also be used in conjunction withelifandelsePerform multiple judgments.
The next is used to obtain page content tags, for example, in the article detail page, we usually usearchiveDetailLabel to obtain the title, content, and keywords of the current article.
The last and most critical is the powerful "filter" function provided by AnQiCMS.These filters can perform various operations on variables, including string operations and format conversions.containFilter is a powerful tool for keyword judgment. It can determine whether a string (or array, Map, etc.) contains a specified keyword and return a boolean value (TrueorFalse), which isifLabel required judgment conditions.
Practical demonstration: Step-by-step implementation of keyword condition display
Now, let's look at a specific example to see how to conditionally display an element in the AnQiCMS template based on whether the content contains a specific keyword.假设我们希望在文章详情页,如果文章正文内容中提到了“AnQiCMS”,就在文章标题下方显示一个醒目的“推荐使用AnQiCMS”提示。
Step 1: Obtain the content to be judged
In the template file of the article detail page (usually)archive/detail.htmlIn a custom document template),we can usearchiveDetailtags to obtain the article content. Usually, we will assign the content to a variable first, for subsequent processing.
{% archiveDetail articleContent with name="Content" %}
{# articleContent 现在包含了文章的正文内容 #}
第二步:Introduce the core tool:containFilter
containThe filter is specifically used to determine whether a string contains a specific substring. Its usage is very direct, the string to be judged is passed through the pipe character|Pass tocontain,followed by the keyword to be searched.
For example:{{ articleContent|contain:"AnQiCMS" }}This will return.TrueorFalse.
Step 3: Combine usage.ifTags andcontainFilter
Now, we will get the content,.containfilters andifcombine the tags to implement conditional judgment.
{% archiveDetail articleContent with name="Content" %}
{% if articleContent|contain:"AnQiCMS" %}
<div style="background-color: #e0f7fa; padding: 10px; border-left: 5px solid #00bcd4; margin-bottom: 20px;">
<p style="font-weight: bold; color: #00838f;">💡 提示:本文内容涉及 AnQiCMS,推荐您深入了解这款高效易用的内容管理系统!</p>
</div>
{% endif %}
In this code block:
- We first use
{% archiveDetail articleContent with name="Content" %}Extract the main content of the current article and store itarticleContenta variable. - Next,
{% if articleContent|contain:"AnQiCMS" %}This line of code judgesarticleContentWhether the variable contains the string "AnQiCMS". - If it contains (i.e., the condition is
True), then{% if ... %}and{% endif %}The HTML code between them will be rendered on the page. - If the article content does not contain “AnQiCMS”, this prompt information will not be displayed on the page.
Through such settings, your website will be able to display specific prompts based on the content of the articles intelligently, without manual intervention, greatly enhancing the convenience of operation and the attractiveness of the content.
Advanced Usage and Flexible Expansion
This keyword-based conditional judgment method can be further expanded to meet more complex scenario requirements:
Multi-keyword Logic Judgment:If you need to judge whether the content contains multiple keywords at the same time (for example, "AnQiCMS" and "Go language"), you can use
andconnect multiplecontainexpression:{% if articleContent|contain:"AnQiCMS" and articleContent|contain:"Go语言" %} {# 同时包含这两个关键词时显示 #} {% endif %}If any of the keywords are included, then use
or:{% if articleContent|contain:"AnQiCMS" or articleContent|contain:"内容管理系统" %} {# 包含其中任一关键词时显示 #} {% endif %}Determine in the article list according to the title/abstract: Besides the article content, you can also use in the article list page
archiveList标签)中,对每个文章的标题(English)进行关键词判断,从而在列表项上添加不同的标签或样式。item.Title) or description (item.Description)进行关键词判断,从而在列表项上添加不同的标签或样式(English)。不区分大小写的关键词匹配(English): By default,
containFilter is case-sensitive. If case-insensitivity is required, you can first uselowerFilter converts both content and keywords to lowercase before judgment:{% set targetKeyword = "anqicms" %} {% if articleContent|lower|contain:targetKeyword %} {# 不区分大小写判断,无论原文是AnQiCMS、anqicms还是ANQICMS都会匹配 #} {% endif %}Dynamic keyword list matching:If you have a set of keywords that need to be matched, you can define a keyword array in the template, then traverse the array for judgment, or concatenate the keywords in the array into a regular expression (if the AnQiCMS template supports regular expression matching), but for
containFor filters, it is more common to pass a single keyword directly. More advanced dynamic matching may require backend data support.
Summary
AnQiCMS with its concise and efficient template syntax and rich built-in features, makes the dynamic content display of the website accessible. By flexibly utilizingifTags andcontainFilters, we can easily implement conditional content display based on keywords, thus injecting more intelligent operational strategies into the website.This ability can be a powerful tool to enhance your website's competitiveness, whether it is to improve user experience, optimize SEO, or refine content marketing.
Common Questions and Answers (FAQ)
1.containIs the filter case-sensitive? If I want to perform a case-insensitive keyword judgment, what should I do?
Yes,containThe filter is case-sensitive by default. For example, checking if “AnQiCMS” contains “cms” will returnFalse. To perform a case-insensitive check, you can first uselowerThe filter converts both the content to be checked and your keywords to lowercase before usingcontainthe judgment. For example:{% if articleContent|lower|contain:"anqicms" %}.
2.I want to judge whether the content contains multiple keywords at the same time, such as it must contain both "A" and "B
AnQiCMS template fully supports complex logical judgments.
If you need to include multiple keywords (i.e., "and" relationship), you can useandoperator to connect multiplecontainexpression:{% if content|contain:"关键词A" and content|contain:"关键词B" %}English.
If you only need to include any of the keywords (i.e., "or" relationship), you can useorOperator:{% if content|contain:"关键词A" or content|contain:"关键词B" %}.
3. Besides the article content (Content), which fields in AnQiCMS can I use for keyword judgment to conditionally display elements?
In addition to the article content, you can also perform keyword judgment on other text fields. For example, on the article detail page, you can usearchiveDetailtags to obtain the title of the article (Title), description (Description) or keywords (Keywords)field for judgment. In the article list page, you can also judge each article item in the loop.item)title.item.Title) or description (item.Description)field for judgment. Through `archiveParams`