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, combiningifThe logical judgment tag performs conditional keyword replacement, making our website content more intelligent and accurate. Whether it is for optimizing SEO effects or providing more attractive information in specific scenarios, this is a very practical skill.
Let's explore how to use this feature in AnQiCMS to revitalize the website content.
Understand Core Tools:ifTag and content processing
In AnQiCMS template syntax,ifTags are the foundation for logical judgment.It allows us to decide whether to execute a piece of code based on set conditions, or to choose one from multiple possibilities.
{% if 条件 %}
<!-- 条件为真时执行的代码 -->
{% elif 其他条件 %}
<!-- 第一个条件不满足,但其他条件为真时执行的代码 -->
{% else %}
<!-- 所有条件都不满足时执行的代码 -->
{% endif %}
In order to implement keyword replacement, we need to use the filter function provided by AnQiCMS. Among them,replaceThe filter can help us replace the specified old word with a new word in a string,containthe filter can be used to determine if the content contains a specific keyword,ifproviding a basis for tag judgment.
replaceFilter:{{ obj|replace:"旧词,新词" }}, it willobjAll "old words" in the variable are replaced with "new words".containFilter:{{ obj|contain:"关键词" }}It will check,objwhether the variable contains the specified "key words" and returnsTrueorFalse.settags:{% set myVar = "some value" %}This tag is very useful, it can help us define temporary variables in templates and make complex logic clearer.
Of course, to operate the content, we first need to get it.archiveDetailto get the document details,categoryDetailGet category details,systemGet global settings and tags, all of which are our sources to obtain target contentobjare the important sources.
Practical scenario: the application of conditional keyword replacement
PassifTags, we can intelligently adjust keywords on the page based on different content attributes or access context.This is more refined and dynamic than the "Full Site Content Replacement" provided by the backend, which is usually used for global, unconditional batch processing of content.The template-level condition replacement is performed immediately before the content is rendered to the user's browser, according to specific rules.
Scenario one: keyword replacement based on classification
Assuming our website sells a variety of products, in the "Mobile Accessories
Inproduct/detail.htmlIn the product details 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 %}
这里我们通过archiveDetailGot the category ID of the current document, thenifUse label judgment, if it is the specified category,replaceFilter performs replacement. Remember to add|safeFilter to ensure that HTML content is parsed correctly, not escaped to plain text.
Scene two: Adjust keywords based on content characteristics
We may wish that if the article title contains the word 'tutorial', then replace 'tutorial' with a more specific 'operation guide' in the article summary to enhance the attractiveness of the content.
Inarticle/list.html(Article list template) orarticle/detail.htmlin the (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 usecontainFilter judgment whether it contains specific keywords and decide whether to replace.
Scenario three: Replace 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 namedIsFeatured(Is Recommended)Boolean custom field, we hope to replace the word 'product' in the 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>
Here we go directly througharchiveDetailTag to get custom fieldsIsFeaturedvalue, and compare it with the string 'Yes' to achieve conditional replacement.
Implementation steps and precautions
- Confirm Replacement Target: Clearly specify which template file (such as
detail.html,list.html) content field (such asTitle,Content,Descriptionor custom field) you want to perform conditional replacement. - Locate the template file: According to AnQiCMS's template directory structure (
design-director.md),find the corresponding template file to edit. For example, the article detail page isarticle/detail.html. - Writing
ifLogical: Combine actual requirements, useif/elif/elseBuild judgment conditions and nestreplaceFilters. If you need to judge the content early