How to use the `replace` filter for string keyword replacement in AnQiCMS templates?

Calendar 👁️ 79

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

Related articles

How to customize the top navigation menu of a website and control its display?

The top navigation menu of the website is the first barrier for users to interact with content, and it directly affects users' understanding of the website structure and content.A well-designed, easy-to-operate navigation menu can significantly improve user experience and website professionalism.AnQiCMS (AnQiCMS) provides flexible and powerful features to help us easily customize and control the top navigation menu of the website to meet different design and operational needs.

2025-11-08

How does AnQiCMS provide visitors with language switching options for content display?

In today's globally interconnected digital age, websites have become a basic need to provide content for users of different languages.No matter whether your business is aimed at the global market or simply wants to cover users in different dialect regions within the country, a website that supports multilingual switching can significantly improve user experience and brand influence.AnQiCMS (AnQiCMS) fully understands this need and provides an efficient and flexible solution to help your website seamlessly switch between multilingual content.

2025-11-08

How to aggregate and display a list of selected articles under multiple categories on a single page?

In website operation, we often encounter such needs: on the homepage or a special topic page, we can display selected articles from different categories, which can enrich the page content and guide users to discover more interesting information.AnQiCMS provides very flexible and powerful features, allowing us to easily achieve this goal without complex programming knowledge.

2025-11-08

How to display the links to the previous and next articles at the bottom of the article detail page?

In website content operation, improving user reading experience is a key link.After a reader has finished browsing an excellent article, if they can immediately see a link to the next related or sequential article, it will undoubtedly greatly increase the likelihood that they will continue to stay on the website.This design not only optimizes the user experience, helps guide users to delve deeper into the website's content, but also effectively reduces the bounce rate and enhances the website's SEO performance, allowing search engines to better understand the structure of your website's content.

2025-11-08

What is the core role of the `replace` filter in AnQiCMS website content display?

In AnQiCMS (AnQiCMS) website content display, the `replace` filter plays a core and practical role, giving content operators the ability to flexibly control the front-end display of content without modifying the original data.In simple terms, this filter helps us replace a specific string in the content with another, thus achieving various refined display adjustments.This function's core value lies in its 'non-intrusiveness' and 'high efficiency'.

2025-11-08

How to accurately replace a specific word in the title or description of AnQiCMS articles?

In website operation, we often encounter the need to uniformly modify the titles or descriptions of a large number of articles.Whether it is a brand renaming, product line adjustment, or keyword strategy update for optimizing search engines (SEO), manually modifying one by one is undoubtedly a time-consuming and labor-intensive task that is prone to errors.Luckily, AnQiCMS has fully considered these operational needs and provided a set of efficient and accurate solutions.

2025-11-08

Can the AnQiCMS `replace` filter achieve dynamic replacement of variable values?

AnQiCMS is an efficient and feature-rich enterprise-level content management system that provides great flexibility to content operators.In the daily content display and processing, we often encounter the need to replace specific text, such as unify brand words, correct misspellings in the content, or dynamically adjust the display content under specific conditions.At this time, the template filter provided by AnQiCMS is particularly important.

2025-11-08

The `replace` filter replaces template content, will it affect the original data in the database?

In daily website operations, we all hope that the content management system can provide powerful functions while ensuring the security and stability of the data.AnQiCMS (AnQiCMS) is an efficient enterprise-level content management system with a flexible template engine and rich filter functions, which brings great convenience to content display.RecentlyFor this question

2025-11-08