When operating content on 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 spelling errors, or adding prefixes/suffixes to specific keywords. At this time,replaceThe filter has become a powerful tool for us to meet these needs. It can help you 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 the AnQiCMS template engine (similar to Django template syntax), its core function is toWhen rendering a template, find the specific keywords in the string (旧关键词)and replace them with another keyword (新关键词)This means you can easily perform accurate text replacement operations in article titles, descriptions, content snippets, and even dynamically generated text, without having to delve into backend data or page logic.

Imagine if your website needs to unify the company brand name from 'AnQi' to 'AnQiCMS', or if there is an old product name in the article content that needs to be updated in bulk, manually modifying tens of thousands of articles is obviously unrealistic. And withreplaceFilter, you just need to specify the replacement rules in the template, and all affected text will be instantly refreshed.

How to usereplaceFilter? Overview of basic syntax.

replaceThe filter usage is very intuitive, 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) and so on.
  • replace:This is the name of the filter we will use.
  • "旧关键词,新关键词":This is passed toreplaceThe filter parameters, which include two parts, usingEnglish comma,Separated.
    • 旧关键词:Where do you want to你的变量Find and replace the string.
    • 新关键词Used to replace.旧关键词The new string.

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

Practical Scenarios and Examples

To help you understand betterreplaceThe power of the filter, we demonstrate its specific usage through several common AnQiCMS template application scenarios.

Scenario one: Uniformly modify the brand name or text

This is one of the most common applications. Suppose your website uses the word "AnQi" a lot, now it needs to be unified as "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 occurrences of 'AnQi' 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. At this point, 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|safeA filter to ensure that the replaced HTML code can be parsed correctly by the browser and not displayed as plain text.

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

This is a special usage. If you set旧关键词to an empty string (i.e., do not enter anything before the comma),replaceThe filter 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 is very useful in some special formatting requirements.

Scenario four: Dynamically adjust the URL path.

When performing SEO optimization or website structure adjustments, 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 page links without modifying the backend URL rules.

UsereplaceFilter considerations

While usingreplaceWhen filtering, there are several key points to keep in mind to ensure the desired effect:

  1. Case sensitive: replaceThe filter isCase sensitiveThis means that:"AnQiCMS"will not match"anqicms"If you need a case-insensitive replacement, you may consider converting the original string and旧关键词to the same case (for example, both to lowercase), and then perform the replacement.
  2. Do not change the original data:Must remember,replaceThe filter only processes the output string immediately when the template is rendered. Itwill not modifyThe original content stored in your AnQiCMS backend database.All replacements only take effect when the user sees the page in their browser, which provides you with great flexibility and security.
  3. Pay attention to the HTML content.|safe:When you operate on a string containing HTML tags (such as article contentarchive.Content),replaceif the string is still HTML content after replacement, it is usually necessary toreplaceafter the filter|safeA filter. This tells the template engine that the output is safe HTML, which does not need to be automatically escaped, thereby preventing HTML tags from being displayed as plain text.
  4. Exact match: 旧关键词It will perform an exact match. If your goal is to replace part of the word, please make sure旧关键词the spelling is consistent with the part you want to match.
  5. Performance considerations:Although AnQiCMS is developed based on 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微小 impact on page rendering time, but for most daily operational needs, this impact is negligible.

Summary

replaceThe filter is a simple and efficient text processing tool in the AnQiCMS template.In order to achieve brand consistency, content revision, or to meet special formatting requirements, it can help you flexibly replace strings at the template level, greatly enhancing the efficiency of content management and the dynamic presentation of pages.Master this filter and it will make you more proficient in the content operation of AnQiCMS.


Frequently Asked Questions (FAQ)

**1