In the content operation practice of AnQiCMS, we often need to refine the output content of the template to ensure that the information displayed to the user is clean, standardized, and meets expectations.This is a common requirement to remove specific characters or symbols from the content.AnQiCMS is a powerful and flexible template engine that provides a rich set of filters to help us achieve these goals, andreplaceThe filter is one of the simple yet very practical tools.

UnderstandingreplaceThe core role of the filter

replaceThe filter, as its name suggests, is mainly used for searching and replacing strings in the template.Its basic logic is very intuitive: find all parts of the target string that match the "old character" and then replace them with the "new character" we specify.This is extremely effective for cleaning up content retrieved from the background, or for standardizing displayed text.

The basic syntax of its usage is as follows:{{ 变量 | replace:"旧字符,新字符" }}

here,变量It is the text content you want to operate on,旧字符This is the specific character or phrase you want to search and replace, while新字符is the content you want to replace旧字符The old character and new character are separated by a comma,It separates.

For example, if you retrieve a title from a database and want to replace specific abbreviations with full names, you can do it like this:

{# 假设 item.Title 的值为 "欢迎使用安企CMS" #}
{{ item.Title | replace:"安企,AnQi" }}
{# 输出结果:欢迎使用AnQiCMS #}

By this simple template code, we can easily replace 'AnQi' in the title and make the brand name more standardized.

Flexible applicationreplaceFilter removes content

replaceOne of the most direct applications of a filter is to "remove" unnecessary characters or symbols from text. The method to achieve this is to set新字符the parameter to an empty string.

1. Remove specific characters or phrases

When you want to completely remove a specific character, word, or phrase from a string, just set新字符Leave the parameter blank. This is very useful when cleaning up marketing tags in article titles, bracketed content in product names, or removing excessive prompts.

Assume there is a document title is "Product Name (New!)", but you do not want to see the part "(New!)" when displaying on the front end:

{# 假设 item.Title 的值为 "产品名称 (新!)" #}
{{ item.Title | replace:" (新!)," }}
{# 输出结果:产品名称 #}

Please note,replaceThe filter performs literal replacement and does not recognize regular expressions. Therefore, if you want to remove characters such as(/)these special symbols, make sure you provide旧字符This is a literal combination of these symbols, not an attempt to use regular expression matching.

2. Clean up extra whitespace characters

Content that is often seen is excessive whitespace, such as multiple spaces between paragraphs in articles or extra newline characters generated when importing content. Although AnQiCMS providestrimFilters are used to handle leading and trailing whitespaces, butreplaceit can be used in a stringInternallyto remove whitespaces of a specific pattern.

For example, if you want to unify multiple consecutive spaces in your text into a single space, you can do it like this:

{# 假设 item.Description 的值为 "这是一个   有    多余   空格的文本。" #}
{{ item.Description | replace:"   , " | replace:"  , " }}
{# 输出结果:这是一个 有 多余 空格的文本。 (通过两次替换,先将三个空格变为一个,再将两个空格变为一个,从而标准化了所有多余空格) #}

Here we use the chain twice.replaceFilter, first replace three consecutive spaces with a single space, then replace two consecutive spaces with a single space.This chained operation is very effective for processing irregular repeated characters.

If you want to completely remove all spaces from the text, you can also do so by making spaces empty:旧字符and set新字符To achieve this:

{# 假设 item.ShortText 的值为 "这是一段没有空格的文字" #}
{{ item.ShortText | replace:" ," }}
{# 输出结果:这是一段没有空格的文字 #}

Note:In the above example,replace:" ,"the intention is to remove spaces. But according to the documentation,filter-replace.mdifoldThe parameter is empty, it will match at the beginning of the string and after each UTF-8 sequence. That is to say,{{"欢迎使用安企CMS"|replace:",-"}}The result is-欢-迎-使-用-安-企-C-M-S-Therefore, if旧字符is a space, it will accurately replace all spaces. If旧字符is an empty string"", then it will insert between each character新字符. So, the correct way to remove all spaces isreplace:" ,"(Note, there is a space before the comma).

3. Handle unnecessary punctuation or special characters.

Sometimes, data imported from the outside may contain punctuation marks or special characters that do not conform to the website style. Usereplacefilters to help us standardize this content.

For example, if you want to remove all exclamation marks in the title of the article!and the question mark?Remove:

{# 假设 archive.Title 的值为 "AnQiCMS 是什么?如何使用!" #}
{{ archive.Title | replace:"!," | replace:"?," }}
{# 输出结果:AnQiCMS 是什么如何使用 #}

We also used chained operations, first removing the Chinese exclamation mark, then removing the Chinese question mark. The operation method is the same for English characters.

Combine with other filters for more complex processing

replaceThe filter can be combined with other AnQiCMS template filters to complete more complex text processing tasks. For example, you may want to clean up HTML tags in the text first, and then remove certain keywords:

{# 假设 page.Content 包含 HTML 内容,并且您想移除其中的“公司”一词 #}
{{ page.Content | striptags | replace:"公司," | safe }}
{# 这里的 safe 过滤器是为了确保最终输出的文本如果还包含其他HTML字符不会被再次转义 #}

In this example,striptagsThe filter will first removepage.Contentall HTML tags, thenreplaceThe filter will then remove the word "company" from the cleaned text.

Use instructions when in use

While usingreplaceThere are several key points to note when using the filter:

  • Case Sensitive: replaceThe filter is case sensitive by default. For example,replace:"cms,CMS"It will not replace 'CMS' with 'CMS'.
  • Literal replacement:As mentioned before,replaceThis is a literal replacement, it does not accept regular expressions. This means you cannot use.or*special characters to match patterns, you must provide the exact string to search for.
  • An empty string as the "old character":When you are going to旧字符The parameter is set to an empty string""then,replaceThe filter will exhibit special behavior, inserting between each UTF-8 character sequence of the original string新字符For example,{{"你好"|replace:",-"}}It will output.-你-好-Therefore, unless you explicitly need this 'insert' behavior, you should avoid setting it to empty.旧字符.
  • Chaining operations:When you need to replace multiple different characters or phrases, you can use the pipe symbol.|chaining callreplaceFilter. Please note that the order of operations may affect the final result.

By processingreplaceFilter