When conducting content operations in AnQiCMS, we often encounter situations where we need to dynamically process the text output by the website template, such as uniformly modifying a brand name, correcting typographical errors, or adding prefixes/suffixes to specific keywords. At this time,replaceFilter becomes a powerful tool for us to meet these needs. It allows you to flexibly adjust the displayed strings on the page without modifying the original content data.

replaceFilter: String Replacement Expert in Templates

replaceThe filter is a powerful feature provided by AnQiCMS template engine (similar to Django template syntax), which plays a core role inWhen rendering templates, find the specific keywords in the string旧关键词) and replace them with another keyword(新关键词).This means you can easily perform precise text replacement operations in article titles, descriptions, content snippets, or even dynamically generated text, without needing to delve into backend data or page logic.

Imagine if your website needs to统一 update the company brand name from "安企" to "AnQiCMS", or if there is an outdated product name in the article content that needs to be updated in bulk. Manually modifying thousands of articles is obviously unrealistic.replaceFilter, you just need to specify the replacement rules in the template, and all affected text will instantly shine new.

How to usereplaceFilter? A quick overview of the basic syntax

replaceThe use of the filter is very intuitive, and its basic syntax is as follows:

{{ 你的变量 | replace:"旧关键词,新关键词" }}

Let's break down this syntax:

  • 你的变量This is the original string you want to replace. It can be direct text, or any string type of data obtained from the AnQiCMS template, such as article titles (archive.Title), category name (category.Title)or system settings(system.SiteName)etc.
  • replace: This is the filter name we will use.
  • "旧关键词,新关键词": This is passed toreplaceThe filter parameters, which contain two parts, separated byEnglish commas,.
    • 旧关键词: What do you want to filter你的变量Search and replace the string.
    • 新关键词Used to replace旧关键词The new string.

Important reminder: replaceThe filter will default replace all matches旧关键词Instead of just replacing the first one.

Practical scenarios and examples

To help you better understandreplaceThe powerful features of the filter, we demonstrate its specific usage through several common AnQiCMS template application scenarios.

Scenario one: Modify brand name or text uniformly.

This is one of the most common applications. Suppose your website uses the word 'AnQi' extensively, and now it needs to be unifiedly replaced with 'AnQiCMS'.

{# 假设有一个字符串变量,或者从后台获取的网站名称 #}
{% set original_text = "欢迎使用安企内容管理系统" %}

<p>{{ original_text | replace:"安企,AnQiCMS" }}</p>

{# 如果是文章标题,可以直接这样用 #}
<h3>{{ archive.Title | replace:"安企,AnQiCMS" }}</h3>

Output result:

<p>欢迎使用AnQiCMS内容管理系统</p>

In this example, all matches of '安企' will be replaced with 'AnQiCMS'.

Scenario two: Remove specific keywords from the content.

Sometimes we may need to completely remove a word from a text. In this case, just新关键词leave it blank.

{# 假设文章描述中包含“过时信息” #}
{% set article_description = "这篇文章包含一些过时信息,请谨慎阅读。" %}

<p>{{ article_description | replace:"过时信息," }}</p>

{# 也可以移除文章内容中的某个关键词 #}
<div class="article-content">
    {{ archive.Content | replace:"内部测试版本," | safe }}
</div>

Output result:

<p>这篇文章包含一些,请谨慎阅读。</p>

By using新关键词set it to an empty string (i.e., do not enter anything after the comma),replaceThe filter will find all "outdated information" and delete it. Note: When replacing HTML content, it is usually necessary to add|safeFilter, to ensure that the replaced HTML code can be correctly parsed by the browser rather than displayed as plain text.

Scenario three: Insert content between each character (advanced technique)

This is a special usage. If you set旧关键词to an empty string (i.e., do not enter any content before the comma),replaceFilter will insert between each UTF-8 character in the original string as well as at the beginning and end of the string新关键词.

{# 假设我们想给一个短语的每个字符之间加上短横线 #}
{% set product_code = "ABC-123" %}

<p>{{ product_code | replace:",-" }}</p>

Output result:

<p>-A-B-C---1-2-3-</p>

This usage is not common, but it can be very useful in some special formatting requirements.

Scenario Four: Dynamically Adjust URL Path

When performing SEO optimization or website structure adjustment, it may be necessary to replace the dynamically generated link fragments on the page.

{# 假设某个链接中含有旧的路径前缀 "/old-path/" #}
{% set product_link = "/old-path/product-detail/item-1.html" %}

<a href="{{ product_link | replace:"/old-path/,/new-path/" }}">查看产品</a>

Output result:

<a href="/new-path/product-detail/item-1.html">查看产品</a>

This is very convenient for temporarily adjusting or testing in-page links without modifying the backend URL rules.

UsereplaceConsiderations for filters

When usingreplaceThe filter has several key points that you need to pay attention to in order to achieve the expected effect:

  1. Case sensitive: replaceThe filter iscase-sensitiveThis means"AnQiCMS"will not match"anqicms"If you need case-insensitive replacement, you may consider converting both the original string and旧关键词to a uniform case (such as all lowercase), and then perform the replacement.
  2. Do not change the original data:Remember to do so,replaceThe filter only processes the output string on-the-fly during template rendering.It does not modifyThe original content stored in your AnQiCMS backend database.All replacements take effect only when the page is displayed in the user's browser, which provides you with great flexibility and security.
  3. Be aware of the HTML content's|safe:When you operate on a string containing HTML tags (such as article contentarchive.Content)replaceIf the string after replacement is still HTML content, it is usually necessary toreplaceafter the filter|safeFilter. This tells the template engine that the output is safe HTML and does not need to be automatically escaped, thus preventing HTML tags from being displayed as plain text.
  4. Exact match: 旧关键词Will perform exact matching. If your goal is to replace part of the words, make sure旧关键词the spelling is exactly the same as the part you want to match.
  5. Performance considerations:Although AnQiCMS is developed in Go language with excellent performance, it is still recommended to use filters moderately when dealing with large amounts of content or very complex replacement logic.Excessive complex string operations may have a slight impact on page rendering time, but this impact is negligible for most daily operational needs.

Summary

replaceThe filter is a concise and efficient text processing tool in AnQiCMS template.Whether it is for brand unification, content revision, or to meet special formatting requirements, it can help you achieve flexible string replacement at the template level, greatly enhancing the efficiency of content management and the dynamic presentation of pages.Master this filter, and you will be more agile in the content operation of AnQiCMS.


Common Questions (FAQ)

**1