In website content operations, we often need to flexibly adjust the display style of the page according to the actual content of the article.For example, if an article mentions a specific product or event, we may want to highlight related purchase links or promotional information on the page; if the content of the article involves sensitive topics, it may be necessary to add additional disclaimers.This requirement for conditional display based on article content is fully achievable in AnQiCMS templates.
The AnQi CMS uses a template engine syntax similar to Django, which makes its templates well-readable and highly capable of logical processing.In AnQiCMS templates, we can easily check if the article content contains the predefined keywords we set through specific tags and filters, and control the display of page elements based on the check results.
How to check if the article content in the AnQiCMS template contains a specific keyword and display it conditionally based on this?
To achieve this goal, we need to use several core features of the AnQi CMS template:
archiveDetailtags: It is used to retrieve detailed information about the current or specified article, including the content of the article.setTags (orwith)The colon is used to define and assign variables in templates, so that we can store the retrieved article content and judgment results.containFilterThis is a key tool for keyword checking, which can determine if one string contains another substring.ifLogical Judgment Tag: Based oncontainThe judgment result (true or false) of the filter to execute different template code blocks.
Below, we will go through specific steps and code examples to elaborate on how to implement this feature in AnQiCMS templates.
I. Retrieve article content
Firstly, we need to obtain the detailed content of the current article in the template. In the article detail page (for examplearchive/detail.html),archiveDetailtags can help us get the articlesContentField. For convenience in subsequent operations, we usually assign the obtained content to a variable.
{# 获取当前文章的Content内容,并赋值给名为 articleContent 的变量 #}
{% archiveDetail articleContent with name="Content" %}
{# 此时,articleContent 变量中就包含了文章的正文HTML内容 #}
Please note,articleContentThe content stored in the variable includes the full article with HTML tags. Usually, you need to add something to display the article content in actuality.|safeFilter to ensure that HTML tags can be correctly parsed by the browser and not displayed as escaped.
Secondly, define the keywords to be checked and make a judgment.
Next, we need to determine the specific keywords to check, and usecontainfilters to judgearticleContentwhether these keywords are included.
containThe filter is very intuitive, its basic usage is{{ obj|contain:关键词 }}It will return a boolean value (TrueorFalse), indicatingobjwhether it contains the specified关键词.
We can imagine thatcontainThe judgment result of the filter is also assigned to a new variable, then combinedifwith tags for conditional display.
1. Check a single keyword
Suppose we want to check if the article content contains the keyword 'Time-limited Offer':
{# 假设我们正在文章详情页 template/archive/detail.html #}
{# 步骤一:获取文章内容并赋值给 articleContent 变量 #}
{% archiveDetail articleContent with name="Content" %}
{# 步骤二:定义要检查的关键词 #}
{% set targetKeyword = "限时优惠" %}
{# 步骤三:使用 contain 过滤器检查文章内容是否包含该关键词,并将结果赋值给 hasOffer 变量 #}
{% set hasOffer = articleContent|contain:targetKeyword %}
{# 步骤四:根据 hasOffer 的值进行条件显示 #}
{% if hasOffer %}
<div class="promo-banner" style="background-color: #fff3cd; padding: 10px; border: 1px solid #ffeeba; margin-bottom: 20px;">
<p style="color: #856404; font-weight: bold;">🎉 注意:本文可能包含 <span style="color: red;">{{ targetKeyword }}</span> 信息,不容错过!</p>
{# 您可以在这里放置一个醒目的促销链接或活动详情 #}
<a href="/promo-page" style="color: #007bff; text-decoration: underline;">查看最新优惠活动 →</a>
</div>
{% endif %}
{# 显示文章正文 #}
<div class="article-main-content">
{{ articleContent|safe }}
</div>
In this example, ifarticleContentThe page top will display a yellow promotional reminder bar if the string 'Limited-time Offer' is included.
2. Check multiple keywords (any of them is sufficient).
If we need to check multiple keywords, as long as the article content contains any of them, the conditional display can be performed, it can be used toorlogic operators to connect multiplecontainjudgments:
{% archiveDetail articleContent with name="Content" %}
{% set keyword1 = "免费试用" %}
{% set keyword2 = "新用户福利" %}
{% set keyword3 = "体验版" %}
{# 只要包含任意一个关键词,hasFreebie 就为 True #}
{% set hasFreebie = articleContent|contain:keyword1 or articleContent|contain:keyword2 or articleContent|contain:keyword3 %}
{% if hasFreebie %}
<div class="freebie-alert" style="background-color: #d1ecf1; padding: 10px; border: 1px solid #bee5eb; margin-bottom: 20px;">
<p style="color: #0c5460;">✨ 本文提及 {{ keyword1 }}、{{ keyword2 }} 或 {{ keyword3 }},快来领取您的专属福利!</p>
<a href="/free-trial" style="color: #007bff; text-decoration: underline;">立即前往 →</a>
</div>
{% endif %}
<div class="article-main-content">
{{ articleContent|safe }}
</div>
3. Check multiple keywords (all must be met)
If the article content must contain all specified keywords simultaneously for conditional display, you can useandLogical operators:
{% archiveDetail articleContent with name="Content" %}
{% set reqKeyword1 = "AnQiCMS" %}
{% set reqKeyword2 = "安装教程" %}
{% set reqKeyword3 = "Go语言" %}
{# 只有同时包含所有关键词,isComprehensive 就为 True #}
{% set isComprehensive = articleContent|contain:reqKeyword1 and articleContent|contain:reqKeyword2 and articleContent|contain:reqKeyword3 %}
{% if isComprehensive %}
<div class="tech-note" style="background-color: #d4edda; padding: 10px; border: 1px solid #c3e6cb; margin-bottom: 20px;">
<p style="color: #155724;">✅ 本文详细介绍了 <span style="font-weight: bold;">{{ reqKeyword1 }} 的 {{ reqKeyword2 }}</span>,并且强调了其 <span style="font-weight: bold;">{{ reqKeyword3 }}</span> 的技术背景。对技术感兴趣的您请仔细阅读!</p>
</div>
{% endif %}
<div class="article-main-content">
{{ articleContent|safe }}
</div>
Section 3: Application Scenario Thinking
This keyword-based conditional display feature has a wide range of applications in actual operation:
- Personalized Recommendations and MarketingIf a product model is mentioned in the article, the sales link of the product can be automatically inserted at the bottom of the article