As an experienced website operations expert, I am well aware that efficient content management and flexible page display are crucial for enhancing user experience and SEO effectiveness.AnQiCMS (AnQiCMS) offers a series of practical tools with its high-performance architecture based on the Go language and the powerful functionality of the Django template engine.filter-containLook how it handles in AnQiCMS'sifJudgment, helping us flexibly check the inclusion relationship of strings or arrays, thus unlocking more intelligent and dynamic content presentation methods.
Secure CMS template tool:filter-containUnlock new ways to judge dynamic content with filters,
In the daily work of content operation, we often need to decide whether to display the content on the page or in what form based on certain conditions.For example, when the title of an article contains specific keywords, we may want to add a "hotfilter-containThe filter is the powerful assistant to solve such "inclusion relationship" judgments, it can simply and efficiently check if there is specific content in strings, arrays, even key-value pairs (map) or structures (struct), and return a boolean value (TrueorFalsein the form returned, perfectly fits our template inifjudgment logic.
filter-contain: The core tool for detecting content inclusion relationships
filter-containThe core function of the filter is to determine whether a target object (which can be a string, array, map, or struct) contains the specified keyword or element. The definition of 'contains' varies according to the type of the target object, but it will always return a clearTrueorFalseLet our template logic become clear and powerful. Its basic usage is very intuitive:{{obj|contain:关键词}}.
Now, let's understand it in detail through several specific scenariosfilter-containactual application.
Check if a string contains specific text
This isfilter-containOne of the most common application scenarios.Imagine, you want to add a prominent corner mark or adjust the style for articles in the list that contain 'AnQiCMS'.filter-contain:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<li>
<a href="{{item.Link}}">
<h5 class="title">{{item.Title}}</h5>
{% if item.Title|contain:"AnQiCMS" %}
<span class="badge">安企CMS相关</span>
{% endif %}
<p>{{item.Description}}</p>
</a>
</li>
{% endfor %}
{% endarchiveList %}
In this code, we traverse the list of articles byitem.Title|contain:"AnQiCMS"Determine whether the current article title contains the keyword "AnQiCMS". If it contains, render a text with the words "AnQiCMS related".spanThe ability to dynamically display content is very useful for enhancing the interactivity and precision of website information display.
2. Determine if a specific element exists in the array (Slice)
In AnQiCMS, we often tag articles or products with multiple tags (Tags), which are usually stored in array form. Suppose we want to check if an article is tagged with the "SEO optimization" tag to display a specific prompt message,filter-containIt can still be used:
{% archiveDetail archive with name="Content" %}
<article>
<h1>{{archive.Title}}</h1>
<div class="article-tags">
{% tagList tags with itemId=archive.Id limit="10" %}
{% set tagTitles = [] %}
{% for tag in tags %}
{% set tagTitles = tagTitles|add:tag.Title %} {# 收集所有标签标题到tagTitles数组 #}
<a href="{{tag.Link}}">{{tag.Title}}</a>
{% endfor %}
{% if tagTitles|contain:"SEO优化" %}
<p class="seo-tip">这篇内容特别针对SEO优化进行了深入探讨!</p>
{% endif %}
{% endtagList %}
</div>
<div class="article-content">
{{archive.Content|safe}}
</div>
</article>
{% endarchiveDetail %}
Here we first obtained all the tag titles of the article and constructed atagTitlesarray, and then usedtagTitles|contain:"SEO优化"Check if the array contains the item 'SEO optimization'. This way, we can dynamically provide additional information or features based on the characteristics of the article.
3. Check if the key-value pair (Map) or structure (Struct) contains a specific key name
filter-containWhen dealing with key-value pairs (map) or structures (struct), their behavior is slightly different, it checks whether a specific key name exists in these objects, rather than checking the key value.This is particularly useful when dealing with custom content models.
For example, in AnQiCMS, we can define custom fields for different content models.Assuming we want to determine on a document detail page whether the content model of the document contains a custom field named "author".
{% archiveDetail archive with name="Content" %}
<article>
<h1>{{archive.Title}}</h1>
{# 假设 archive.Extra 是一个包含所有自定义字段键值对的结构体或map #}
{% archiveParams params with sorted=false %}
{% if params|contain:"author" %}
<p>作者:{{ params.author.Value }}</p>
{% endif %}
{% if params|contain:"source" %}
<p>来源:{{ params.source.Value }}</p>
{% endif %}
{% endarchiveParams %}
<div class="article-content">
{{archive.Content|safe}}
</div>
</article>
{% endarchiveDetail %}
In this example,archiveParamsThe tag obtained all the custom parameters of the document and used them asparamsVariable passed. Throughparams|contain:"author"We judgeparamsWhether there exists a key named 'author' in this key-value pair (or structure). If it exists, we can safely access itparams.author.ValueTo display the author information, it can avoid template rendering errors due to the absence of fields.
Expansion of operation strategy application
filter-containFlexible application, can bring many conveniences to our content operation:
- Intelligent SEO optimization: By checking whether the article title and description contain core keywords, SEO-friendly prompts can be dynamically added, or the page structure can be adjusted to assist search engines in better understanding the content.
- Personalized content recommendation: Based on user behavior or tag preferences, combine the tags or attributes of the content to dynamically filter and display a list of content that is more in line with user interests.
- Dynamic feature switchIn a multi-site or multi-template scenario, you can decide whether to load a specific JavaScript, CSS, or feature module based on certain characteristics of the current site or template (such as the existence of certain system settings).
- Content Quality ControlFor example, check if the user's submitted comment contains certain sensitive words, or if the article content conforms to specific requirements (although such checks are usually performed on the backend, but auxiliary hints during frontend display are also useful).
Summary
filter-containThe filter is a powerful and practical tool in the AnQiCMS template engine, it greatly simplifies the complexity of our content conditional judgments with its intuitive syntax and boolean return mechanism.Whether it is to check the inclusion of simple strings or to explore specific elements or key names in arrays and key-value pairs, it can help us build a smarter and more dynamic AnQiCMS website.Master and effectively use this filter, no doubt it will make your website operation more efficient, flexible, and provide users with a more customized content experience.
Frequently Asked Questions (FAQ)
Q1:filter-containAre string or array comparisons case-sensitive?A1: Yes,filter-containString contains comparisons are case-sensitive. For example,"AnQiCMS"|contain:"cms"will returnFalse. If you need to perform a case-insensitive judgment, you may need to convert the target string and keywords to the same case (such as lowercase) before judgment, but AnQiCMS built-inlowerorupperThe filter can assist in achieving this, for example{{ item.Title|lower|contain:"anqicms" }}.
Q2: Can I usefilter-containDo you want to check the relationship between multiple keywords at once? For example, do I want to know if an article contains both 'SEO' and 'marketing'?A2:filter-containYou can only check one keyword at a time. If you need to check a situation where multiple keywords are included, you can combine multiplecontainPass judgmentifThe logical operator of tags (such asandoror) together. For example: