In the flexible and powerful template system of AnQiCMS, achieving high content customization is the key to improving user experience and meeting operational needs. Among them, combiningifLogical judgment tags for conditional keyword replacement can make our website content more intelligent and accurate. Whether it is to optimize SEO effects or to provide more attractive information in specific scenarios, this is a very practical skill.

Let's explore how to use this feature in AnQiCMS to bring new vitality to the website content.

Understanding core tools:ifTag and content processing

In the template syntax of AnQiCMS,ifTags are the foundation for logical judgments. They allow us to decide whether to execute a piece of code based on set conditions, or to choose one from multiple possibilities.Its basic structure is similar to the programming languages we are familiar with:

{% if 条件 %}
    <!-- 条件为真时执行的代码 -->
{% elif 其他条件 %}
    <!-- 第一个条件不满足,但其他条件为真时执行的代码 -->
{% else %}
    <!-- 所有条件都不满足时执行的代码 -->
{% endif %}

In order to implement keyword replacement, we need to make use of the filter function provided by AnQiCMS. Among which,replaceThe filter can help us replace the specified old word with a new one in the string, andcontainThe filter can be used to determine if the content contains a certain keyword, toifprovide a basis for tag judgment.

  • replaceFilter:{{ obj|replace:"旧词,新词" }}, it will be used toobjAll 'old words' in the variable are replaced with 'new words'.
  • containFilter:{{ obj|contain:"关键词" }}It will check,objwhether the variable contains the specified 'keyword' and returnTrueorFalse.
  • setTag:{% set myVar = "some value" %}This tag is very useful, it can help us define temporary variables in templates, making complex logic clearer.

Of course, to manipulate content, we first need to get it.archiveDetailUsed to obtain document details,categoryDetailGet category details,systemGet global settings and other tags, which are all our sources to obtain target contentobjAn important source.

Practical scenario: The application of conditional keyword replacement

ByifLabel, we can intelligently adjust the keywords on the page based on different content attributes or access context.This is more refined and dynamic than the 'Global Content Replacement' provided by the backend, which is usually used for unconditional, batch processing of global content.Template-level conditional replacement is performed before the content is rendered to the user's browser, based on specific rules and immediately.

Scene one: Keyword replacement based on classification

Assuming our website sells a variety of products, under the 'Mobile Accessories' category, we hope to replace the word 'high quality' in the product description with 'original factory quality' to emphasize its professionalism.

Inproduct/detail.htmlIn the product detail template, we can operate like this:

{# 首先获取当前产品的分类ID #}
{% archiveDetail currentCategoryId with name="CategoryId" %}

{% if currentCategoryId == 15 %} {# 假设“手机配件”分类的ID是15 #}
    {# 如果是手机配件分类,则替换关键词 #}
    {% archiveDetail productContent with name="Content" %}
    <div class="product-description">
        {{ productContent|replace:"高品质,原厂级品质"|safe }}
    </div>
{% else %}
    {# 其他分类则显示原始内容 #}
    {% archiveDetail productContent with name="Content" %}
    <div class="product-description">
        {{ productContent|safe }}
    </div>
{% endif %}

Here we go througharchiveDetailGot the category ID of the current document, thenifTag judgment, if it is the specified category, usereplaceFilter to replace. Remember to add.|safeFilter to ensure that HTML content is parsed correctly and not escaped into plain text.

Scenario two: Adjust keywords based on content characteristics

We may hope that if the article title contains the word 'tutorial', then in the article abstract, 'tutorial' should be replaced with a more specific 'operation guide' to enhance the attractiveness of the content.

Inarticle/list.htmlOr (article list template)article/detail.htmlIn (article detail template):

{% archiveList articles with type="list" limit="10" %} {# 假设在文章列表页 #}
{% for item in articles %}
    <div class="article-item">
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>
            {% set originalDescription = item.Description %}
            {% if originalDescription|contain:"教程" %}
                {# 如果摘要包含“教程”,则替换为“操作指南” #}
                {{ originalDescription|replace:"教程,操作指南" }}
            {% else %}
                {# 否则显示原始摘要 #}
                {{ originalDescription }}
            {% endif %}
        </p>
    </div>
{% endfor %}

In this example, we first usesettags to store the article description in a temporary variable, then usecontainThe filter determines whether to include a specific keyword and then decide whether to replace it.

Scenario three: Replace combined with custom fields.

AnQiCMS supports adding custom fields to content models. We can use these fields to control keyword replacement. For example, if the product model has a field namedIsFeaturedThe boolean custom field (whether recommended), we hope to replace the word "product" in its name when the product is marked as recommended with "recommended product".

AssumeIsFeaturedThe field is set to 'Single choice' in the background, with values 'Yes' or 'No'.

{# 在产品详情页,获取自定义字段“IsFeatured”的值 #}
{% archiveDetail isFeaturedValue with name="IsFeatured" %}

<h3>
    {% if isFeaturedValue == "是" %} {# 判断自定义字段的值 #}
        {{ archive.Title|replace:"产品,推荐商品" }}
    {% else %}
        {{ archive.Title }}
    {% endif %}
</h3>

We go through it directly herearchiveDetailTag to get custom fieldIsFeaturedThe value, and compare it with the string "is" to achieve conditional replacement.

Implementation steps and precautions

  1. Determine the replacement target: Clearly specify which template file (such asdetail.html,list.htmlWhich content field (such asTitle,Content,Descriptionor custom field) for conditional replacement.
  2. Locate the template file: According to the AnQiCMS template directory structure (design-director.md), find the corresponding template file to edit. For example, the article detail page is usuallyarticle/detail.html.
  3. Writingiflogic: Combine actual needs, useif/elif/elsebuild judgment conditions and nestreplaceFilter. If you need to judge the content early