As an experienced website operations expert, I know that the flexible use of templates is the key to improving website efficiency and user experience in daily content management.English CMS (EnglishCMS) provides us with powerful content customization capabilities with its efficient architecture based on the Go language and support for Django template engine syntax.{% if "关键词" in 变量 %}This judgment method implementation.
The content in the Anqi CMS template includes judgment: master.containThe magic of filters
In the actual operation of the website, we often encounter such needs: to decide the display method, recommended content, or even trigger specific interaction logic on the page, based on whether the content of the article, title, or custom field contains a specific keyword. For example, we may want:
- If the article title contains the words 'Promotion', then add a prominent 'Hot Sale' label.
- If the product description mentions 'Time-limited Offer', then display a countdown module.
- Based on whether the keyword list of the article contains a certain industry term, recommend related in-depth reading.
These scenarios revolve around a core question: How to accurately and efficiently judge whether a variable (whether it is a string, array, or object) contains the 'keywords' we are concerned about in the AnQiCMS template?
The template system of AnQi CMS is similar to Django in syntax, but it extends the processing capability of variables by providing a set of rich and practical filters (Filters). For checking the content relationship, we cannot directly use Python-styleinOperator, rather than using AnQiCMS built-incontainFilter.
UnderstandingcontainThe core function of the filter
containTrueorFalse), this is what we need for conditional judgment.
Its basic syntax is very concise, usually used like this:
{{ 变量 | contain:"关键词" }}
Or, when we need to{% if %}Use it in conditional judgments, it will fit in perfectly:
{% if 变量 | contain:"关键词" %}
<!-- 如果变量包含关键词,这里的内容就会显示 -->
{% endif %}
Next, we will look at several common application scenarios to see how the filter shines in the AnQiCMS template.containHow the filter makes a big difference in the AnQiCMS template.
Practice Session:containMultiple applications of the filter
Scene one: Check if the article title or content contains specific words
Assuming we are developing an article detail page, we hope to display information based on the article title (archive.TitleDoes it contain “AnQiCMS” to display a special prompt message?
{% archiveDetail archiveContent with name="Title" %}
<h1 class="article-title">{{ archiveContent }}</h1>
{% if archiveContent|contain:"AnQiCMS" %}
<p class="highlight-info">✨ 这篇文章特别推荐,因为它提到了强大的 AnQiCMS!</p>
{% endif %}
{% endarchiveDetail %}
Similarly, if we want to check the main content of the article (archive.ContentDoes it contain a certain word. Since the article content is usually rich text, it may contain HTML tags, in order to ensurecontainThe filter processes plain text or the text we hope to have, and can be correctly parsed when outputting HTML, we may need to combine with other filters, such asstriptagsTo strip HTML, or directly use inarchive.Contentwhen used directly withsafeFilter:
{% archiveDetail article with name="Content" %}
<div class="article-content">
{{ article|safe }} {# 输出文章内容,并确保HTML被解析 #}
{% if article|striptags|contain:"GoLang" %}
<p class="tech-note">🔍 文章内容中包含 GoLang 相关技术讨论。</p>
{% endif %}
</div>
{% endarchiveDetail %}
Here,article|striptagsIt will first strip the HTML tags, then perform keyword extractionGoLangThe judgment, which can avoid the situation where keywords are separated by HTML tags and cannot be matched.
Scenario two: Judge the attributes of the article based on the keywords (Keywords) of the article.
auto CMS allows us to set keywords for articles (archive.Keywords) these keywords are usually stored in the form of a comma-separated string. We can utilizecontainTo determine whether the article belongs to a specific topic.
{% archiveDetail articleKeywords with name="Keywords" %}
{% if articleKeywords|contain:"SEO优化" %}
<div class="seo-badge">🚀 SEO优化必读!</div>
{% elif articleKeywords|contain:"内容营销" %}
<div class="marketing-badge">💡 内容营销策略精选!</div>
{% endif %}
{% endarchiveDetail %}
In this example,articleKeywordsis a string,containThe filter will directly search for "SEO optimization" or "content marketing" in this string.
If a field returns an array (for example, we get an array throughsplitThe filter converts comma-separated strings into an array),containThis also applies. For example, suppose we have a virtualcategory_tagsArray:
”`twig {% set category_tags =