In the daily content management and template design of Anqi CMS, we often encounter scenarios where we need to search and replace specific content in strings.Whether it is to unify brand names, filter sensitive words, or adjust the display format of certain texts, manually modifying a large amount of content is undoubtedly cumbersome and inefficient.replaceIt can help us easily implement these string operations.

replaceFilter: Your string processing buddy

replaceThe filter is a powerful tool built into the AnQi CMS template engine, which core function is to find all matches of "old words" in the specified string and replace them with "new words" in English.This process is completely automated during template rendering, requiring no intervention from you in the backend database or manual editing of each article, greatly enhancing the flexibility and efficiency of content operation.

Imagine if your website needs to replace all occurrences of '安企' with 'AnQi', or if you need to remove a specific phrase that is no longer in use,replaceThe filter comes in handy. It allows you to flexibly control the final display effect of the content without touching the original data.

How to usereplaceFilter?

replaceThe syntax of the filter usage is very intuitive and simple. It is usually attached to the variable you want to process and provided in the form of colons separating 'old word' and 'new word'.

The basic syntax format is as follows:

{{ obj|replace:"old,new" }}

Here:

  • objThis represents the original string variable you want to search and replace. It can be the article title, content description, custom field, or any other string type of data.
  • old:is the one you hope to findobjand replace the specific keyword in.
  • new:is the new keyword you use to replaceoldwith.

Please pay special attention to,oldandnewBetween them must be usedEnglish commas,Split the content.

Let us understand its basic usage through a simple example. Suppose you have a variablesiteName,其值为“Welcome to AnQi CMS”,now we want to replace “AnQi” with “AnQi”:

{{ "欢迎使用安企CMS"|replace:"安企,AnQi" }}

AfterreplaceAfter the filter is processed, the display result of the page will be:

欢迎使用AnQiCMS

Flexible Application Scenarios and Practical Examples

replaceFilters are not limited to simple 'one-to-one' replacements, they can also achieve some more clever string operations.

1. Conventional Keyword Replacement

This isreplaceThe most common usage, which replaces a specific word in a string with another. This is very helpful for standardizing terminology, updating brand names, or correcting common errors.

Example: Replace the old brand name with the new brand name in the article content.

Assuming the content variable of one of your documents isarticleContentThe word “old company name” appeared multiple times, and it needs to be unified to display as “new company name”:

{# 假设 articleContent 变量内容为 "旧公司名称发布了新产品,旧公司名称市场前景广阔。" #}
{{ articleContent|replace:"旧公司名称,新公司名称" }}

After processing, the displayed content will be:

新公司名称发布了新产品,新公司名称市场前景广阔。

2. Insert content between each character

When you are going tooldThe old keyword is set to an empty string,replaceThe filter will become very interesting. It will insert the specified characters at the beginning of the original string and after each UTF-8 sequence (i.e., each character).new(New keyword). This usage is very useful when it is necessary to format text in a special way.

Example: Insert a hyphen between each character of the text.-.

If you have a string "Welcome to Anqi CMS" and want to add a hyphen between each character, you can do it like this:

{{ "欢迎使用安企CMS"|replace:",-" }}

After processing, the display result on the page will be:

-欢-迎-使-用-安-企-C-M-S-

Please note that this method will also add characters at the beginning of the stringnew. If you do not want to add characters at the beginning, you may need to combine other string processing methods (such assliceortrimLeft) to remove the characters at the beginning.

3. Remove specific content from a string

If your goal is to directly remove a certain keyword from a string rather than replacing it with other content, you can simply:newEnglish: (New keyword) Set to an empty string.

Example: Delete specific adjectives from the article title.

Assuming you have an article title variablearticleTitleAmong them, it often contains the words “(Exclusive)”, but you want to remove it in some display scenarios:

{# 假设 articleTitle 变量内容为 "安企CMS(独家)发布全新功能" #}
{{ articleTitle|replace:"(独家)," }}

After processing, the article title will be displayed as:

安企CMS发布全新功能

UsereplaceTips for the filter

  • Case sensitive: replaceThe filter is case-sensitive.This means that 'CMS' and 'cms' are considered different words.If you need case-insensitive replacement, it is usually necessary to preprocess the data (such as converting to a uniform case) when obtaining the data, or to implement it in the background management.
  • Non-destructive operation: replaceThe filter only affects the display effect during template rendering, it does not change the original content stored in your background database.This provides secure content display control without worrying about accidental modification of the source data.
  • Chained use:If you need to perform multiple different replacement operations on the same string, you can concatenate multiplereplacefilters in series, for example:{{ obj|replace:"old1,new1"|replace:"old2,new2" }}.

Frequently Asked Questions (FAQ)

Q1:replaceFilter whether it supports regular expressions for more complex matching and replacement?

A1:English CMS template ofreplaceThe filter design philosophy is simple and efficient, mainly used for simple string to string replacement.According to the document description, it does not support regular expressions.If you need to use regular expressions for advanced pattern matching and replacement, you may need to consider preprocessing your content before publishing through the backend management functions (such as the 'Document Keyword Replacement' feature in document management) or by using more powerful server-side scripts.

Q2: Do I need to write many different keywords to replace multiple different ones?replaceFilter?

A2:Yes, if you have multiple unrelated keywords to replace, the most direct method is to use multiple as mentioned above in a chained mannerreplacefilter. For example:

{{ articleContent|replace:"旧公司名称,新公司名称"|replace:"旧产品系列,新产品系列"|replace:"(已过期)," }}

This ensures that each replacement operation is executed in order, thereby achieving the final effect you expect.

Q3: Why did I usereplaceFilter, but the content on the page has not changed?

A3:If you encounter this situation, you can check from the following aspects:

  1. Keyword matching issue:Make sure youoldThe keyword in the parameter matches the content in the original string exactly, including case, spaces, or special characters.replaceThe filter matches exactly.
  2. Comma-separated:CheckoldandnewWhether the correct is used betweenEnglish commas,Use a separator. Chinese commas or other characters can cause the filter to fail to parse correctly.
  3. Is the variable correct:Confirm whether you want to operateobjWhether the expected string content has been correctly retrieved. It can be{{ obj }}output the variable separately to check its original value.
  4. Template Caching:Sometimes, even if the template file is modified, the browser or system may have cache.Try clearing the browser cache or cleaning the system cache in the "Update Cache" feature of the Anqi CMS backend, and then refresh the page to view the effect.