In AnqiCMS, flexibly judging and dynamically displaying information based on content is the key to improving website user experience and SEO effectiveness.Imagine a website that automatically displays related purchase links based on whether an article mentions a specific product; or recommends different sidebar content based on the category the user is in.This not only makes your website smarter, but also greatly improves the efficiency of content operation.

The AnqiCMS template engine provides powerful features to easily implement these dynamic logic.It borrows the syntax of the Django template engine, making conditional judgment and data traversal intuitive.Today, let's delve into how to determine whether a string, array, or object in the AnqiCMS template contains a specific keyword and display content dynamically based on this.

Understand the judgment logic in AnqiCMS templates

The core of AnqiCMS templates lies in their concise and powerful tags. Variables are usually enclosed in double curly braces.{{变量}}wrap, while logical control (such as conditional judgment and loop) uses single curly braces and percentage signs{% 标签 %}when it is necessary to display content dynamically according to dataiflogical judgment tag{% if ... %}It is our basic tool, it allows us to define conditions and execute corresponding template code when the conditions are true.

However, just havingifThe label is not enough.We need a method to determine whether a variable contains a specific value.containfilter.

Core tool:containFilter

containThe filter is a tool specifically used in the AnqiCMS template engine to detect whether the content contains specific keywords.Its functionality is very powerful, not only can it determine whether a line of string contains a keyword, but it can also detect whether an element exists in an array (slice), and even whether a key exists in a key-value pair (map) or a structure (struct).

containThe basic usage of the filter is very simple: you just need to pass the variable to be checked through the pipe character|pass tocontainFilter, and specify the keyword you want to count after the colon.:and then specify the keyword you want to search for.

For example:{{ obj|contain:"关键词" }}

This filter will return a boolean value (TrueorFalseIf the keyword is found, it returnsTrue; otherwise, it returnsFalse. With this boolean value, we can combineiftags to easily implement dynamic content display.

The practical application scenario: display dynamically according to content

Let's look at some specific examplescontainThe application of filters under different data types.

1. Determine if a text string contains a specific keyword

This is the most common application scenario. For example, you want to display a special promotional icon or link if the title or description of an article contains the words 'discount'.

{% archiveDetail article with name="Description" %} {# 获取当前文章的描述内容 #}

{% if article|contain:"优惠" %}
    <span class="promo-badge">限时优惠!</span>
    <a href="/promotions" class="btn btn-primary">立即查看优惠商品</a>
{% else %}
    <a href="/products" class="btn btn-secondary">浏览所有商品</a>
{% endif %}

In this example,archiveDetailThe tag obtained the description of the current page article. Then, we usearticle|contain:"优惠"To determine if the description string contains the word "discount". If it does, display promotional information; otherwise, display a normal product link.

2. Determine if a specific element exists in the array (Slice)

Sometimes, you will encounter list type data, such as the Tag list of an article.You may want to display a 'NEW' corner mark if the article is marked as 'new'.

AssumetagsIt is an array containing strings, for example.["GoLang", "CMS", "SEO"].

{% tagList tags with itemId=archive.Id limit="10" %} {# 获取当前文章的标签列表 #}

{% if tags|contain:"新品" %}
    <span class="new-badge">NEW</span>
{% endif %}

{# 也可以遍历标签列表,判断每个标签的Title属性 #}
{% for tag in tags %}
    {% if tag.Title|contain:"新品" %}
        <span class="new-badge">NEW</span>
    {% endif %}
{% endfor %}

here,tagListThe tag fetched all the tags associated with the current article.tags|contain:"新品"It will check if the element 'New Product' is present in the array of tag names. Of course, iftagsis an array of objects (such astag.TitleYou may need to traverse the array and judge a specific attribute of each element.

3. Determine whether a specific key name exists in the object (Map/Struct)

Custom fields in AnqiCMS are very flexible, you can add any custom fields to articles or products through the content model.Assuming you have added a custom field named "VideoLink" to the product model, used to store the link of the product demonstration video.If this field exists, you would want to embed a video player on the page.

{% archiveParams params with sorted=false %} {# 获取当前文章的所有自定义参数,以map形式返回 #}

{% if params|contain:"VideoLink" %}
    <div class="product-video">
        <h3>产品演示</h3>
        <iframe src="{{ params.VideoLink.Value }}" frameborder="0" allowfullscreen></iframe>
    </div>
{% else %}
    <p>暂无产品演示视频。</p>
{% endif %}

In this example,archiveParamsThe tag retrieved all the custom parameters of the article.params|contain:"VideoLink"the judgment.paramsDoes this object contain a key named 'VideoLink'. If it exists, we can go throughparams.VideoLink.ValueGet the video link and embed the player.

Flexible combination: Make content presentation more intelligent and accurate

containthe filter meetsif/else/elifThe combination of tags can achieve very complex dynamic content logic.For example, you can display different sidebar recommended articles, call different CSS styles, and even change the overall layout of the page based on different keywords in the article content.

{% archiveDetail articleContent with name="Content" %} {# 获取文章完整内容 #}

{# 如果内容包含“联系我们”且包含“定制服务”,显示定制服务表单 #}
{% if articleContent|contain:"联系我们" and articleContent|contain:"定制服务" %}
    <div class="custom-service-form">
        <h4>定制服务需求提交</h4>
        {% guestbook fields %}
            {# 这里嵌入留言表单的字段 #}
        {% endguestbook %}
    </div>
{# 如果只包含“联系我们”,显示基础联系信息 #}
{% elif articleContent|contain:"联系我们" %}
    <div class="contact-info">
        <h4>联系我们</h4>
        <p>电话:{% contact with name="Cellphone" %}</p>
        <p>邮箱:{% contact with name="Email" %}</p>
    </div>
{% else %}
    <p>如果您有任何疑问,请随时<a href="/contact">联系我们</a>。</p>
{% endif %}

By such a combination, your